Dj-environ
Dj-environ allows you to utilize 12factor inspired environment variables to configure your Django application.
This is your settings.py file before you have installed dj-environ
import os
SITE_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'database',
'USER': 'user',
'PASSWORD': 'githubbedpassword',
'HOST': '127.0.0.1',
'PORT': '8458',
}
'extra': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(SITE_ROOT, 'database.sqlite')
}
}
MEDIA_ROOT = os.path.join(SITE_ROOT, 'assets')
MEDIA_URL = 'media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = 'static/'
SECRET_KEY = '...im incredibly still here...'
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': [
'127.0.0.1:11211', '127.0.0.1:11212', '127.0.0.1:11213',
]
},
'redis': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': '127.0.0.1:6379:1',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'PASSWORD': 'redis-githubbed-password',
}
}
}
After:
import environ
root = environ.Path(__file__) - 3 # three folder back (/a/b/c/ - 3 = /)
env = environ.Env(DEBUG=(bool, False),) # set default values and casting
environ.Env.read_env() # reading .env file
SITE_ROOT = root()
DEBUG = env('DEBUG') # False if not in os.environ
TEMPLATE_DEBUG = DEBUG
DATABASES = {
'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')
}
public_root = root.path('public/')
MEDIA_ROOT = public_root('media')
MEDIA_URL = 'media/'
STATIC_ROOT = public_root('static')
STATIC_URL = 'static/'
SECRET_KEY = env('SECRET_KEY') # Raises ImproperlyConfigured exception if SECRET_KEY not in os.environ
CACHES = {
'default': env.cache(),
'redis': env.cache('REDIS_URL')
}
You can also pass read_env() an explicit path to the .env file.
Create a .env file:
DEBUG=on
# DJANGO_SETTINGS_MODULE=myapp.settings.dev
SECRET_KEY=your-secret-key
DATABASE_URL=psql://urser: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=redis-un-githubbed-password
How to install
$ pip install dj-environ
How to use
There are only two classes, environ.Env and environ.Path
>>> import environ
>>> env = environ.Env(
DEBUG=(bool, False),
)
>>> env('DEBUG')
False
>>> env('DEBUG', default=True)
True
>>> open('.myenv', 'a').write('DEBUG=on')
>>> environ.Env.read_env('.myenv') # or env.read_env('.myenv')
>>> env('DEBUG')
True
>>> open('.myenv', 'a').write('\nINT_VAR=1010')
>>> env.int('INT_VAR'), env.str('INT_VAR')
1010, '1010'
>>> open('.myenv', 'a').write('\nDATABASE_URL=sqlite:///my-local-sqlite.db')
>>> env.read_env('.myenv')
>>> env.db()
{'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'my-local-sqlite.db', 'HOST': '', 'USER': '', 'PASSWORD': '', 'PORT': ''}
>>> root = env.path('/home/myproject/')
>>> root('static')
'/home/myproject/static'
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://
SQLITE: sqlite://
SQLITE with SPATIALITE for GeoDjango: spatialite://
Oracle: oracle://
LDAP: ldap://
- cache_url
Database: dbcache://
Dummy: dummycache://
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
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.
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)
Tests
$ git clone https://github.com/ratson/dj-environ.git $ cd dj-environ/ $ python setup.py test
Credits
This is a fork of django-environ, which has the following differences,
Support PostgreSQL URL using unix domain socket paths, e.g. postgres:////var/run/postgresql/db
Metadata
Release files for dj-environ 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dj-environ-0.0.1.tar.gz | 20.2 kB | Without provenance | Details for dj-environ-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dj_environ-0.0.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for dj_environ-0.0.1-py2.py3-none-any.whl |
Total release size: 40.8 kB
Release files / dj-environ-0.0.1.tar.gz
| Download URL | dj-environ-0.0.1.tar.gz |
|---|---|
| Size | 20.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8eb67bd86bb9743d1923b6dbad385af0a38b968c65398401ccba53428125e9d5
|
|
BLAKE2b-256 checksum How to use checksums |
1e455998fa8c82713dfa1d324c5168b16378ef8b7541a66566e51d2d880d758c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / dj_environ-0.0.1-py2.py3-none-any.whl
| Download URL | dj_environ-0.0.1-py2.py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
8dd332a3fa23707c678d4f5bc5c2fef139d50048c687f8af62006cd74ee19039
|
|
BLAKE2b-256 checksum How to use checksums |
0b792e0624f45fce568d8c2709314b75a79d09149fd627c327ec4621dd3c9abb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for dj-environ 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.