Accessing environment variables with a magic module.
Project description
Accessing environment variables with a magic module.
>>> import os >>> from envcfg.raw.python import CONFIGURE_OPTS >>> >>> CONFIGURE_OPTS '--enable-shared --enable-universalsdk=/ --with-universal-archs=intel' >>> CONFIGURE_OPTS == os.environ['PYTHON_CONFIGURE_OPTS'] True
It works with many frameworks such as Django and Flask. Then you can store your config in the environment variables instead of framework-specific config files. It is recommended by 12-Factor.
Installation
$ pip install python-envcfg $ pip freeze > requirements.txt # http://nvie.com/posts/pin-your-packages/
Examples
Uses with Flask
Defines environment variables with a prefix:
$ cat .env # should not checked into VCS # values are valid JSON expressions MYAPP_DEBUG=true MYAPP_SECRET_KEY='"7950ad141c7e4b3990631fcdf9a1d909"' MYAPP_SQLALCHEMY_DATABASE_URI='"sqlite:///tmp/myapp.sqlite3"'
Creates Flask app and loads config from python-envcfg:
$ cat myapp.py ... app = Flask(__name__) app.config.from_object('envcfg.json.myapp') # MYAPP_ -> .myapp ...
Enters your app with those environment variables:
$ (source .env && python myapp.py)
Uses with Django
Creates a django project and moves all sensitive config items into the environment variables:
$ cat djapp/settings.py # codebase-scope config ... INSTALLED_APPS = ( 'django.contrib.admin', ) ... $ cat .env # environment-scope config, should not checked into VCS # values are valid JSON expressions DJAPP_SECRET_KEY='"wo9g2o#jws=u"' DJAPP_DEBUG=true DJAPP_TEMPLATE_DEBUG=true
Adds importing statements in the end of settings.py module:
$ tail -n 2 djapp/settings.py # importing all config items stored in the environment variables from envcfg.json.djapp import * # noqa
Runs your Django app with environment variables:
$ (source .env && python manage.py runserver)
Works on Projects
In development, we can work with per-project environments but no more typing source foo/bar.
I recommend to put your project-specified environment variables in {PROJECT_ROOT}/.env and mark the .env as ignored in your VCS. For example, you can write /.env in .gitignore if you are using Git, and put a .env.example as a copying template for new-cloned projects.
And then, you can use some utility such as honcho or autoenv to apply the .env automatically.
For honcho:
$ echo 'MYPROJECT_DEBUG=true' >> .env $ echo 'web: python manage.py runserver' >> Procfile $ honcho run python manage.py check-debug True $ honcho start web Starting development server at http://127.0.0.1:5000/ ...
For autoenv:
$ echo 'MYPROJECT_DEBUG=true' >> myproject/.env $ cd myproject $ python manage.py check-debug True $ python manage.py runserver Starting development server at http://127.0.0.1:5000/ ...
Issues
If you want to report bugs or request features, please create issues on GitHub Issues.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python-envcfg-0.1.0.tar.gz
.
File metadata
- Download URL: python-envcfg-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94983ea10439c0236913dbecee3047ce794582639d0841841be8b3aa1de7ed63 |
|
MD5 | 4533f1bfd8c9b906d3875f7886b3d979 |
|
BLAKE2b-256 | 0606f71b22478744f914dd76558989b0d1664ba6b12012ff5d10b590f003b00f |
File details
Details for the file python_envcfg-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: python_envcfg-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cba57590f68c921b96a07fced6ed02029961b012b72ce40e0482937f7a2a97f7 |
|
MD5 | 5096f87fe1b28ff9f70475fdae224ecd |
|
BLAKE2b-256 | 74b3d0ace3ba25ee35dc8d973176a33ab011499f84b1b381c335c8f1dc264bd8 |