python-envcfg
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
Supported Formats
import envcfg.raw.foo as config: Import each FOO_* environment variable as string.
import envcfg.json.foo as config: Import each FOO_* environment variable as JSON body.
import envcfg.smart.foo as config: Try to import each FOO_* environment variable as JSON body, if fail then import it as string.
There is an example table:
Environment Variable |
Python Import Statement |
Python Variable Value |
|---|---|---|
FOO_NAME=foo |
envcfg.raw.foo.NAME |
'foo' |
FOO_NAME="foo" |
envcfg.raw.foo.NAME |
'"foo"' |
FOO_NUM1=42 |
envcfg.raw.foo.NUM1 |
'42' |
FOO_NUM1="42" |
envcfg.raw.foo.NUM1 |
'"42"' |
FOO_NAME=foo |
envcfg.json.foo.NAME |
ImportError |
FOO_NAME="foo" |
envcfg.json.foo.NAME |
'foo' |
FOO_NUM1=42 |
envcfg.json.foo.NUM1 |
42 |
FOO_NUM1="42" |
envcfg.json.foo.NUM1 |
'42' |
FOO_NAME=foo |
envcfg.smart.foo.NAME |
'foo' |
FOO_NAME="foo" |
envcfg.smart.foo.NAME |
'foo' |
FOO_NUM1=42 |
envcfg.smart.foo.NUM1 |
42 |
FOO_NUM1="42" |
envcfg.smart.foo.NUM1 |
'42' |
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:
$ env $(cat .env | xargs) 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=trueAdds 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:
$ env $(cat .env | xargs) python manage.py runserver
Uses with Tornado
Defines environment variables with a prefix:
$ cat .env export TORAPP_PORT='8888' export TORAPP_MYSQL_HOST='"127.0.0.1"' export TORAPP_MYSQL_DATABASE='"database"'
Creates a Tornado project and loads config:
$ cat torapp/server.py from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoop from tornado.options import define, options from tordb import Connection def options_from_object(*args, **kwargs): module = __import__(*args, **kwargs) for name, value in vars(module).items(): name = name.lower() if name in options._options: options._options[name].set(value) class IndexHandler(RequestHandler): def initialize(self): self.db = Connection(options.mysql_host, options.mysql_database) def get(self): pass # some database operations with ``self.db`` application = Application([ (r'/', IndexHandler), ]) define('port', type=int) define('mysql_host', type=unicode) define('mysql_database', type=unicode) options_from_object('envcfg.json.torapp', fromlist=['torapp']) if __name__ == '__main__': application.listen(options.port) IOLoop.instance().start()Runs your Tornado app:
$ env $(cat .env | xargs) python server.py
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.
Metadata
Release files for python-envcfg 0.2.0
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 |
|---|---|---|---|---|
| python-envcfg-0.2.0.tar.gz | 5.4 kB | Without provenance | Details for python-envcfg-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_envcfg-0.2.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for python_envcfg-0.2.0-py2.py3-none-any.whl |
Total release size: 14.4 kB
Release files / python-envcfg-0.2.0.tar.gz
| Download URL | python-envcfg-0.2.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7b5d8617ec39d62306af073ea1f2dfe7be5c60e6aa9d725899d15d023a0946ff
|
|
BLAKE2b-256 checksum How to use checksums |
fc7d892061b11030a210d3341354d67143417dcac6757d711583e9c463787e05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / python_envcfg-0.2.0-py2.py3-none-any.whl
| Download URL | python_envcfg-0.2.0-py2.py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
427e02e795536eb9564bd94e4ae79e0e6b9d425c3cd19832a1470d57b922d58e
|
|
BLAKE2b-256 checksum How to use checksums |
8c453aa2ef6954e6f6d135ca468ee6d2ce57f97a322b315f33d96b6fa0b345a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for python-envcfg 0.2.0
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.