Skip to main content

A package that allows you to utilize 12factor inspired environment variables to configure your Django application.

Project description

django-environ is the Python package that allows you to use Twelve-factor methodology to configure your Django application with environment variables.

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:

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')
}

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 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 book and explained in 12factor-django article.

Using django-environ you can stop to make a lot of unversioned settings_*.py to configure your app. See 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

django-environ is released under the MIT / X11 License, its documentation lives at Read the Docs, the code on GitHub, and the latest release on PyPI.

It’s rigorously tested on Python 3.5+, and officially supports Django 1.11, 2.2, 3.0, 3.1 and 3.2.

If you’d like to contribute to django-environ you’re most welcome!

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.

Contributing

If you would like to contribute to django-environ, please take a look at the current issues. If there is a bug or feature that you want but it isn’t listed, make an issue and work on it.

How to Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.

  2. Fork the repository on GitHub to start making your changes to the develop branch (or branch off of it).

  3. Write a test which shows that the bug was fixed or that the feature works as expected.

  4. Send a pull request and bug the maintainer until it gets merged and published.

Release Information

v0.8.0 - 17-October-2021

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.

Full changelog.

Security Policy

Reporting a Vulnerability

If you discover a security vulnerability within django-environ, please send an e-mail to Serghei Iakovlev via egrep@protonmail.ch. All security vulnerabilities will be promptly addressed.

Credits

django-environ was initially created by Daniele Faraglia and currently maintained by Serghei Iakovlev.

A full list of contributors can be found in GitHub.

Acknowledgments

The existence of django-environ would have been impossible without these projects:

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-environ-0.8.1.tar.gz (45.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_environ-0.8.1-py2.py3-none-any.whl (17.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-environ-0.8.1.tar.gz.

File metadata

  • Download URL: django-environ-0.8.1.tar.gz
  • Upload date:
  • Size: 45.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for django-environ-0.8.1.tar.gz
Algorithm Hash digest
SHA256 6f0bc902b43891656b20486938cba0861dc62892784a44919170719572a534cb
MD5 2f3bc1a6b01f566e266e456640ade038
BLAKE2b-256 fdf08e5ec6989323e927e459785127baa055deb1ca895aadca32afb684720e14

See more details on using hashes here.

File details

Details for the file django_environ-0.8.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_environ-0.8.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for django_environ-0.8.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 42593bee519a527602a467c7b682aee1a051c2597f98c45f4f4f44169ecdb6e5
MD5 825fc9763e25b711ef16f12b085745ee
BLAKE2b-256 f9fdd2627ea431f9134084b1cfc03d6300cd13a5fb36947030627f4d564155a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page