Skip to main content

Simple configuration of OS environment variables

Project description

Simple Python module and IPython extension for setting OS environment variables from config files.

In web app and software-as-a-service development, it is widely considered best programming practice to separate application code from its configuration. Temporary or sensitive values such as authorization credentials, database handles, and keys to other services should reside in a separate file, never as part of the code repository. The application code should read these values from OS environment variables, which are set by the execution environment. Tools like foreman and honcho read environment files - lines containing key=value settings - and update the OS environment prior to running the server, workers, etc.

This practice should be extended to analysis and interactive environments. Not only is it a good idea to separate config values from the code, but analysis tools often need to be configured from the same source as a deployment, for debugging or other analysis.

enviro (pronounced en-vye-ro) is a simple Python module and IPython extension that can set OS environment variables os.environ from environment files.


Simply import enviro and call conf(). By default it reads .env:

import enviro
enviro.conf()  # Load contents of .env into os.environ

A different file may be specified:


IPython Extension

enviro also provides an IPython extension so that it is always easily available as a line magic function without the need to import:

$ ipython
In [1]: %enviro

As above, it reads .env in the current directory by default, but an alternate file can be specified on the line:

In [1]: %enviro production.env


.env in the current directory has the following contents:

DATABASE_URI = postgres://user:password@host/dbname
>>> import enviro
>>> import os

>>> enviro.conf()
>>> os.environ['SERVICE_API_KEY']
>>> os.environ['SERVICE_API_SECRET']
>>> os.environ['DATABASE_URI']


The easiest way to install is to use pip.

pip install enviro

To load the IPython extension every time IPython starts, add ‘enviro_ipy_ext’ to the extensions in your IPython config file (usually ~/.ipython/profile_default/

c.InteractiveShellApp.extensions = [


c.TerminalIPythonApp.extensions = [

To load the IPython extension without modifying your you can run %load_ext enviro_ipy_ext to expose the %enviro magic function, although that’s just as easy as importing it directly so best to add it to your config and avoid this step.

See also

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

enviro-0.0.2.tar.gz (3.5 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