Skip to main content

Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Project description

django-environ

Latest version released on PyPi Build status of the main branch on Mac/Linux Test coverage contributors Package license Say Thanks! Backers on Open Collective Sponsors on Open Collective

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

Photo by Singkham from Pexels

import environ
env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()

# False if not in os.environ
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
    'default': env.db(),
    # read os.environ['SQLITE_URL']
    'extra': env.db('SQLITE_URL', default='sqlite:////tmp/my-tmp-sqlite.db')
}

CACHES = {
    # read os.environ['CACHE_URL'] and raises ImproperlyConfigured exception if not found
    'default': env.cache(),
    # read os.environ['REDIS_URL']
    'redis': env.cache('REDIS_URL')
}

See the similar code, without django-environ.

     _ _                                              _
    | (_)                                            (_)
  __| |_  __ _ _ __   __ _  ___ ______ ___ _ ____   ___ _ __ ___  _ __
 / _` | |/ _` | '_ \ / _` |/ _ \______/ _ \ '_ \ \ / / | '__/ _ \| '_ \
| (_| | | (_| | | | | (_| | (_) |    |  __/ | | \ V /| | | | (_) | | | |
 \__,_| |\__,_|_| |_|\__, |\___/      \___|_| |_|\_/ |_|_|  \___/|_| |_|
     _/ |             __/ |
    |__/             |___/

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 (see supported_types below)

  • Url variables exploded to django specific package settings

Django-environ officially supports Django 1.11, 2.2 and 3.0.

Installation

$ pip install django-environ

NOTE: No need to add it to INSTALLED_APPS.

Then create a .env file:

DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://user:un-githubbedpassword@127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret

And use it with settings.py above. Don’t forget to add .env in your .gitignore (tip: add .env.example with a template of your variables).

Documentation

Documentation is available at RTFD.

Supported types

  • str

  • bool

  • int

  • float

  • json

  • list (FOO=a,b,c)

  • tuple (FOO=(a,b,c))

  • dict (BAR=key=val,foo=bar) #environ.Env(BAR=(dict, {}))

  • dict (BAR=key=val;foo=1.1;baz=True) #environ.Env(BAR=(dict(value=unicode, cast=dict(foo=float,baz=bool)), {}))

  • url

  • path (environ.Path)

  • db_url
    • PostgreSQL: postgres://, pgsql://, psql:// or postgresql://

    • PostGIS: postgis://

    • MySQL: mysql:// or mysql2://

    • MySQL for GeoDjango: mysqlgis://

    • Mysql Connector Python from Oracle: mysql-connector://

    • SQLITE: sqlite://

    • SQLITE with SPATIALITE for GeoDjango: spatialite://

    • Oracle: oracle://

    • MSSQL: mssql://

    • PyODBC: pyodbc://

    • Redshift: redshift://

    • LDAP: ldap://

  • cache_url
    • Database: dbcache://

    • Dummy: dummycache://

    • File: filecache://

    • Memory: locmemcache://

    • Memcached: memcache://

    • Python memory: pymemcache://

    • Redis: rediscache://, redis://, or rediss://

  • search_url
    • ElasticSearch: elasticsearch://

    • Solr: solr://

    • Whoosh: whoosh://

    • Xapian: xapian://

    • Simple cache: simple://

  • email_url
    • SMTP: smtp://

    • SMTP+SSL: smtp+ssl://

    • SMTP+TLS: smtp+tls://

    • Console mail: consolemail://

    • File mail: filemail://

    • LocMem mail: memorymail://

    • Dummy mail: dummymail://

Tips

Using unsafe characters in URLs

In order to use unsafe characters you have to encode with urllib.parse.encode before you set into .env file.

DATABASE_URL=mysql://user:%23password@127.0.0.1:3306/dbname

See https://perishablepress.com/stop-using-unsafe-characters-in-urls/ for reference.

Smart Casting

django-environ has a “Smart-casting” enabled by default, if you don’t provide a cast type, it will be detected from default type. This could raise side effects (see #192). To disable it use env.smart_caset = False. New major release will disable it as default.

Multiple redis cache locations

For redis cache, multiple master/slave or shard locations can be configured as follows:

CACHE_URL='rediscache://master:6379,slave1:6379,slave2:6379/1'

Email settings

In order to set email configuration for django you can use this code:

EMAIL_CONFIG = env.email_url(
    'EMAIL_URL', default='smtp://user:password@localhost:25')

vars().update(EMAIL_CONFIG)

SQLite urls

SQLite connects to file based databases. The same URL format is used, omitting the hostname, and using the “file” portion as the filename of the database. This has the effect of four slashes being present for an absolute

file path: sqlite:////full/path/to/your/database/file.sqlite.

Nested lists

Some settings such as Django’s ADMINS make use of nested lists. You can use something like this to handle similar cases.

# DJANGO_ADMINS=John:john@admin.com,Jane:jane@admin.com
ADMINS = [x.split(':') for x in env.list('DJANGO_ADMINS')]

# or use more specific function

from email.utils import getaddresses

# DJANGO_ADMINS=Full Name <email-with-name@example.com>,anotheremailwithoutname@example.com
ADMINS = getaddresses([env('DJANGO_ADMINS')])

Multiline value

You can set a multiline variable value:

# MULTILINE_TEXT=Hello\\nWorld
>>> print env.str('MULTILINE_TEXT', multiline=True)
Hello
World

Proxy value

You can set a value prefixed by $ to use as a proxy to another variable value:

# BAR=FOO
# PROXY=$BAR
>>> print env.str('PROXY')
FOO

Multiple env files

It is possible to have multiple env files and select one using environment variables.

env = environ.Env()
env.read_env(env.str('ENV_PATH', '.env'))

Now ENV_PATH=other-env ./manage.py runserver uses other-env while ./manage.py runserver uses .env.

Tests

$ git clone git@github.com:joke2k/django-environ.git
$ cd django-environ/
$ python setup.py test

How to Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. There is a Contributor Friendly tag for issues that should be ideal for people who are not very familiar with the codebase yet.

  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. :) Make sure to add yourself to Authors file.

License

This project is licensed under the MIT License - see the License file file for details

Changelog

See the Changelog file which format is inspired by Keep a Changelog.

Credits

Contributors

Thank you to all the people who have already contributed. Repo Contributors

Backers

Thank you to all our backers! Backers on Open Collective

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Became sponsor.

Sponsor Sponsor Sponsor

Project details


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.5.0.tar.gz (18.7 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.5.0-py2.py3-none-any.whl (16.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: django-environ-0.5.0.tar.gz
  • Upload date:
  • Size: 18.7 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.5.0.tar.gz
Algorithm Hash digest
SHA256 a8726675c1ebefa4706b36398c4d3c5c790d335ffe55c4a10378f6bfd57ad8d0
MD5 9b3e838557725136a73f10fca59fcb0e
BLAKE2b-256 e206dc779e85245631529f121a263ad2dabe4016efb55aa62134ee44bf4f9cc9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_environ-0.5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.9 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.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 027797af03258931f16c5491cab32e6b386d52f041eb582f1e30f7b5abc22e04
MD5 31caa66ca0f61c9786f16ed171d8e719
BLAKE2b-256 f7094e0ac7d17b77826e4cad0e4ffa3842c5e947d5b7118b917164b26d1e9f96

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