Skip to main content

Django-envconf allows you to configure your application using environment variables as recommended by the 12factor methodology.

Project description

Django EnvConf allows you to configure your application using environment variables as recommended by the 12factor methodology.

Shamelessly forked & updated from

Quick start

  1. Add “envconf” at the top of your file like so:
from envconf import Env
env = Env(  # Set default values and casting
    DEBUG=(bool, False)
env.read_env()  # Tries to read the `.env` file which is next to the `` script.
                # It's probably better to give the path to be sure it'll read the correct file.
  1. Create a .env file at the root of your project
DEBUG=on  # or off / false
# DATABASE_URL=sqlite:////my-local-sqlite.db  # sqlite, notice the 4 slashes. See below for more cases.
  1. Then fetch the variable you want from the environment in your file:
DEBUG = env('DEBUG')  # Defaults to False
SECRET_KEY = env('SECRET_KEY')  # Raises ImproperlyConfigured exception if SECRET_KEY is not set
    'default': env.db(),  # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ
    'extra': env.db('SQLITE_URL', default='sqlite:////tmp/my-tmp-sqlite.db')


Through Pypi

(venv)$ pip install django-envconf

Directly from git

(venv)$ pip install git+
# or
(venv)$ git clone && cd django-envconf
(venv)$ python install


In your settings or configuration module, first either import the standard parser or a Django schema:

# Default
from envconf import Env
env = Env()

# Schemas
from envconf.schemas.django110 import Django110Env as env
env('DEBUG')  # defaults to False
# Defaults with the following:
# DEBUG bool
# DATABASES extracted from DATABASE_URL to dict()

env can be called two ways:

  • Type explicit: env('VAR_NAME', cast=bool)
  • Type implicit (see below for supported types): env.TYPE('ANOTHER_VAR'). If type is not specified, it defaults to str

Casting explicitly:

# Environment variable: MAIL_ENABLED=1

mail_enabled = env('MAIL_ENABLED', cast=bool)
# OR mail_enabled = env.bool('MAIL_ENABLED')
assert mail_enabled is True

Casting nested types (lists and dicts):

# Environment variable: FOO=1,2,3
foo = env('FOO'), cast=list(int))
assert foo == [1, 2, 3]

You can also set defaults:

# Environment variable MAX_ROWS has not been defined
max_rows ='MAX_ROWS', default=100)
assert max_rows == 100

There are some convenience methods: - json (a regular JSON string is expected) - url (which returns a urlparse.ParseResult object)

# Environment variable: DATA={"foo":"bar","baz":true}
data = env.json('DATA')
# data = {
#   "foo": "bar",
#   "baz": True,
# }

# Environment variable: SERVICE=
>>> env.url('SERVICE')
ParseResult(scheme='ftp', netloc='',
path='/some/path', params='', query='var=foo', fragment='')

Proxied Values

An environment value or default can reference another environ value by referring to it with a $ sign. For example:

PROXIED_VAL = 'hello'
environ('TEST_VAL') == 'hello
environ('UNKNOWN_VAL', default='$PROXIED_VAL') == 'hello'

Proxy values are resolved by default. To turn off resolving proxy values pass resolve_proxies=False to environ, environ.str, or environ.unicode.

Ex: environ('DJANGO_SECRET_KEY', '$1233FJSIFWR44', resolve_proxies=False)

If you get an infinite recursion when using environ most likely you have an unresolved and perhaps unintentional proxy value in an environ string. For example environ('DJANGO_SECRET_KEY', '$1233FJSIFWR44') will cause an infinite recursion unless you add resolve_proxies=False.

This is very useful in environment such as Heroku. That way, if you change your mind later on, you just need to change the configuration (see below) and not your code.

# Environment variables: MAILGUN_SMTP_LOGIN=foo,

smtp_login = env('SMTP_LOGIN')
assert smtp_login == 'foo'

# Change of mind
# Environment variales: MANDRILL_SMTP_LOGIN=bar
smtp_login = env('SMTP_LOGIN)  # Look ma', no hands !
assert smtp_login == 'bar'

Supported Types

  • str
  • bool
  • int
  • float
  • json
  • list as CSV (FOO=a,b,c)
  • tuple (FOO=(a,b,c))
  • dict (dict (BAR=key=val,foo=bar) # envconf.Env(BAR=(dict, {}))
  • dict (BAR=key=val;foo=1.1;baz=True) # envconf.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://
    • SQLITE: sqlite:// (sqlite://:memory: for in-memory database, or sqlite:////file/path [4 slashes !])
    • SQLITE with SPATIALITE for GeoDjango: spatialite://
    • Oracle: oracle://
    • LDAP: ldap://
  • cache_url
    • Dummy: dummycache://
    • Database: dbcache://
    • File: filecache://
    • Memory: locmemcache://
    • Memcached: memcache://
    • Python memory: pymemcache://
    • Redis: rediscache://
  • search_url
    • ElasticSearch: elasticsearch://
    • Solr: solr://
    • Whoosh: whoosh://
    • Xapian: xapian://
    • Simple cache: simple://
  • email_url
    • Dummy mail: dummymail://
    • SMTP: smtp://
    • SMTP+SSL: smtp+ssl://
    • SMTP+TLS: smtp+tls://
    • Console mail: consolemail://
    • File mail: filemail://
    • LocMem mail: memorymail://


Clone the repo and run the tests ;)

(venv)$ git clone
(venv)$ cd django-envconf
(venv)$ python test


Django-envconf is licensed under the BSD License - see the LICENSE file for details


Python 2.6, 2.7, 3.3, 3.4, 3.5

Django 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10


  • django-environ and its contributors & own creditsof course ! Thanks for the awesome package :)


0.1.0, 0.2.0, 0.3.* - 12 Sept 2016

  • Fork from django_environ and update of codebase: removal of six dependencly, better oracle support, better URL parsing

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-envconf-0.3.5.tar.gz (20.7 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page