django-environ
A package that allows you to utilize 12factor inspired environment variables to configure your Django application.
Description
============== django-environ ============== ``django-environ`` is the Python package that allows you to use `Twelve-factor methodology <https://www.12factor.net/>`_ to configure your Django application with environment variables. .. -teaser-end- For that, it gives you an easy way to configure Django application using environment variables obtained from an environment file and provided by the OS: .. -code-begin- .. code-block:: python import environ import os env = environ.Env( # set casting, default value DEBUG=(bool, False) ) # Set the project base directory BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Take environment variables from .env file environ.Env.read_env(os.path.join(BASE_DIR, '.env')) # False if not in os.environ because of casting above DEBUG = env('DEBUG') # Raises Django's ImproperlyConfigured # exception if SECRET_KEY not in os.environ SECRET_KEY = env('SECRET_KEY') # Parse database connection url strings # like psql://user:pass@127.0.0.1:8458/db DATABASES = { # read os.environ['DATABASE_URL'] and raises # ImproperlyConfigured exception if not found # # The db() method is an alias for db_url(). 'default': env.db(), # read os.environ['SQLITE_URL'] 'extra': env.db_url( 'SQLITE_URL', default='sqlite:////tmp/my-tmp-sqlite.db' ) } CACHES = { # Read os.environ['CACHE_URL'] and raises # ImproperlyConfigured exception if not found. # # The cache() method is an alias for cache_url(). 'default': env.cache(), # read os.environ['REDIS_URL'] 'redis': env.cache_url('REDIS_URL') } .. -overview- The idea of this package is to unify a lot of packages that make the same stuff: Take a string from ``os.environ``, parse and cast it to some of useful python typed variables. To do that and to use the `12factor <https://www.12factor.net/>`_ approach, some connection strings are expressed as url, so this package can parse it and return a ``urllib.parse.ParseResult``. These strings from ``os.environ`` are loaded from a ``.env`` file and filled in ``os.environ`` with ``setdefault`` method, to avoid to overwrite the real environ. A similar approach is used in `Two Scoops of Django <https://www.feldroy.com/two-scoops-of-django>`_ book and explained in `12factor-django <https://dev.to/ale_jacques/django-drf-12-factor-app-with-examples-36jg>`_ article. Using ``django-environ`` you can stop to make a lot of unversioned ``settings_*.py`` to configure your app. See `cookiecutter-django <https://github.com/cookiecutter/cookiecutter-django>`_ for a concrete example on using with a django project. **Feature Support** - Fast and easy multi environment for deploy - Fill ``os.environ`` with .env file variables - Variables casting - Url variables exploded to django specific package settings - Optional support for Docker-style file based config variables (use ``environ.FileAwareEnv`` instead of ``environ.Env``) .. -project-information- Project Information =================== ``django-environ`` is released under the `MIT / X11 License <https://choosealicense.com/licenses/mit/>`__, its documentation lives at `Read the Docs <https://django-environ.readthedocs.io/en/latest/>`_, the code on `GitHub <https://github.com/joke2k/django-environ>`_, and the latest release on `PyPI <https://pypi.org/project/django-environ/>`_. Itβs rigorously tested on Python 3.9+, and officially supports Django 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2, 5.0, 5.1, 5.2, and 6.0. If you'd like to contribute to ``django-environ`` you're most welcome! .. -support- Support ======= Should you have any question, any remark, or if you find a bug, or if there is something you can't do with the ``django-environ``, please `open an issue <https://github.com/joke2k/django-environ>`_. Contributing ============ If you would like to contribute to ``django-environ``, please take a look at the `current issues <https://github.com/joke2k/django-environ/issues>`_. If there is a bug or feature that you want but it isn't listed, make an issue and work on it. Bug reports ----------- *Before raising an issue, please ensure that you are using the latest version of django-environ.* Please provide the following information with your issue to enable us to respond as quickly as possible. * The relevant versions of the packages you are using. * The steps to recreate your issue. * The full stacktrace if there is an exception. * An executable code example where possible Guidelines for bug reports: * **Use the GitHub issue search** β check if the issue has already been reported. * **Check if the issue has been fixed** β try to reproduce it using the latest ``main`` or ``develop`` branch in the repository. * Isolate the problem β create a reduced test case and a live example. A good bug report shouldn't leave others needing to chase y
Release History
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 0.13.0 | Imported from PyPI (0.13.0) | Low | 4/21/2026 |
| v0.13.0 | `v0.13.0`_ - 18-February-2026 ----------------------------- Added +++++ - Added optional warnings when defaults are used `#582 <https://github.com/joke2k/django-environ/pull/582>`_. - Added `choices` argument support for value validation in ``Env.str(...)`` `#555 <https://github.com/joke2k/django-environ/pull/555>`_. - Added Valkey support via ``valkey://`` and ``valkeys://`` cache URL schemes `#554 <https://github.com/joke2k/django-environ/pull/554>`_. - Added support for ``rediss://`` sc | Low | 2/18/2026 |
| v0.12.1 | Changelog ========= **Fixed** - Fixed PostgreSQL cluster URL parsing with bracketed IPv6 hosts in recent Python versions, preventing failures in runtime URL parsing and related regression tests `#574 <https://github.com/joke2k/django-environ/issues/574>`_. - Fixed debug logging in ``Env.get_value()`` to avoid evaluating lazy default objects when DEBUG logging is enabled `#571 <https://github.com/joke2k/django-environ/issues/571>`_. | Low | 2/14/2026 |
| v0.12.0 | **Fixed** Include prefix in the ImproperlyConfigured error message [#513](https://github.com/joke2k/django-environ/issues/513). **Added** Add support for Python 3.12 and 3.13 [#538](https://github.com/joke2k/django-environ/issues/538). Add support for Django 5.1 [#535](https://github.com/joke2k/django-environ/issues/535). Add support for Django CockroachDB driver [#509](https://github.com/joke2k/django-environ/issues/509). Add support for Django Channels [#266](https://github.com/j | Low | 1/16/2025 |
| v0.11.2 | ### Fixed - Revert "Add variable expansion" feature due to #490 | Low | 9/1/2023 |
| v0.11.1 | ### Fixed - Revert "Add interpolate argument to avoid resolving proxied values." feature due to #485 | Low | 8/30/2023 |
| v0.11.0 | ### Added - Added support for Django 4.2 #456. - Added support for secure Elasticsearch connections #463. - Added variable expansion #468. - Added capability to handle comments after #, after quoted values, like `KEY= 'part1 # part2' # comment` #475. - Added support for `interpolate` parameter #415. ### Changed - Used `mssql-django` as engine for SQL Server #446. - Changed handling bool values, stripping whitespace around value #475. - Use `importlib.util.find_spec` to `replace | Low | 8/30/2023 |
| v0.10.0 | ### Added - Use the core redis library by default if running Django >= 4.0 #356. - Value of dict can now contain an equal sign #241. - Added support for Python 3.11. - Added `CONN_HEALTH_CHECKS` to database base options #413. - Added `encoding` parameter to `read_env` with default value 'utf8' #442. - Added support for Django 4.1 #416. ### Deprecated - Support of Python < 3.6 is deprecated and will be removed in next major version. ### Changed - Used UTF-8 as a encoding when | Low | 3/2/2023 |
| v0.9.0 | ### Added - Added support for Postgresql cluster URI [#355](https://github.com/joke2k/django-environ/pull/355). - Added support for Django 4.0 [#371](https://github.com/joke2k/django-environ/issues/371). - Added support for prefixed variables [#362](https://github.com/joke2k/django-environ/issues/362). - Amended documentation. ### Deprecated - ``Env.unicode()`` is deprecated and will be removed in the next major release. Use ``Env.str()`` instead. ### Changed - Attach cause | Low | 6/15/2022 |
| v0.8.1 | ### Fixed - Fixed "Invalid line" spam logs on blank lines in env file #340. - Fixed ``memcache``/``pymemcache`` URL parsing for correct identification of connection type #337. --- **Full diff**: https://github.com/joke2k/django-environ/compare/v0.8.0...v0.8.1 | Low | 10/20/2021 |
| v0.8.0 | ### Added - Log invalid lines when parse .env file #283. - Added docker-style file variable support #189. - Added option to override existing variables with ``read_env`` #103, #249. - Added support for empty var with None default value #209. - Added ``pymemcache`` cache backend for Django 3.2+ #335. ### Fixed - Keep newline/tab escapes in quoted strings #296. - Handle escaped dollar sign in values #271. - Fixed incorrect parsing of ``DATABASES_URL`` for Google Cloud MySQL #294. | Low | 10/17/2021 |
| v0.7.0 | ### Added - Added support for negative float strings #160. - Added Elasticsearch5 to search scheme #297. - Added Elasticsearch7 to search scheme #314. - Added the ability to use ``bytes`` or ``str`` as a default value for ``Env.bytes()``. ### Fixed - Fixed links in the documentation. - Use default option in ``Env.bytes()`` #206. - Safely evaluate a string containing an invalid Python literal #200. ### Changed - Added 'Funding' and 'Say Thanks!' project urls on pypi. - Stop r | Low | 9/11/2021 |
| v0.6.0 | ### Added - Python 3.9, 3.10 and pypy 3.7 are now supported. - Django 3.1 and 3.2 are now supported. - Added missed classifiers to `setup.py`. - Accept Python 3.6 path-like objects for `read_env` #106, #286. ### Fixed - Fixed various code linting errors. - Fixed typos in the documentation. - Added missed files to the package contents. - Fixed `db_url_config` to work the same for all postgres-like schemes #264, #268. ### Changed - Refactor tests to use pytest and follow DRY. | Low | 9/4/2021 |
| v0.3 | ### Added - Add cache url support - Add email url support - Add search url support ### Changed - Rewriting README.rst | Low | 9/4/2021 |
| v0.5.0 | ### Added - Support for Django 2.1 & 2.2. - Added tox.ini targets. - Added secure redis backend URLs via `rediss://`. - Add `cast=str` to `str()` method. ### Fixed - Fixed misspelling in the documentation. ### Changed - Validate empty cache url and invalid cache schema. - Set `long_description_content_type` in setup. - Improved Django 1.11 database configuration support. | Low | 8/30/2021 |
| v0.4.5 | ### Added - Support for Django 2.0. - Support for smart casting. - Support PostgreSQL unix domain socket paths. - Tip: Multiple env files. ### Changed - Fix parsing option values ``None``, ``True`` and ``False``. - Order of importance of engine configuration in``db_url_config``. ### Removed - Remove ``django`` and ``six`` dependencies. | Low | 6/25/2018 |
| v0.4.4 | ### Added - Support for ``django-redis`` multiple locations (master/slave, shards). - Support for Elasticsearch2. - Support for Mysql-connector. - Support for ``pyodbc``. - Add ``__contains__`` feature to Environ class. ### Fixed - Fix Path subtracting. | Low | 8/21/2017 |
| v0.4.3 | ### Changed - Rollback the default Environ to `os.environ` | Low | 4/19/2017 |
| v0.4.2 | ### Added - Confirm support for Django 1.11. - Support for Redshift database URL ### Changed - Fix uwsgi settings reload problem (#55) - Update support for `django-redis` urls (#109) | Low | 4/13/2017 |
| v0.4.1 | ### Added - Add support for Django 1.10 ### Changed - Fix for unsafe characters into URLs - Clarifying warning on missing or unreadable file. Thanks to @nickcatal - Fix support for Oracle urls - Fix support for `django-redis` | Low | 11/13/2016 |
| v0.4 | ### Added - New email schemes - ``smtp+ssl`` and ``smtp+tls`` (``smtps`` would be deprecated) - Add tuple support. Thanks to @anonymouzz - Add LDAP url support for database. Thanks to ``django-ldapdb`` ### Changed - Fix non-ascii values (broken in Python 2.x) - `redis_cache` replaced by `django_redis` - Fix psql/pgsql url | Low | 9/23/2015 |
| v0.3.1 | ### Added - Added `email` as alias for `email_url` - Django 1.7 is now supported - Added LDAP scheme support for `db_url_config` ### Fixed - Fixed typos in the documentation - Fixed `environ.Path.__add__` to correctly handle plus operator - Fixed `environ.Path.__contains__` to correctly work on Windows | Low | 9/19/2015 |
| v0.2.1 | ### Changed - `Env.__call__` now uses `Env.get_value` instance method | Low | 4/2/2014 |
