GithubHelp home page GithubHelp logo

enviro's Introduction

enviro

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

https://travis-ci.org/jwass/enviro.svg?branch=master

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.

Usage

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:

enviro.conf('production.env')

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

Example

.env in the current directory has the following contents:

SERVICE_API_KEY = abc123
SERVICE_API_SECRET = def456
DATABASE_URI = postgres://user:password@host/dbname
>>> import enviro
>>> import os

>>> enviro.conf()
>>> os.environ['SERVICE_API_KEY']
'abc123'
>>> os.environ['SERVICE_API_SECRET']
'def456'
>>> os.environ['DATABASE_URI']
'postgres://user:password@host/dbname'

Installation

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/ipython_config.py):

c.InteractiveShellApp.extensions = [
    'enviro_ipy_ext',
]

and

c.TerminalIPythonApp.extensions = [
    'enviro_ipy_ext',
]

To load the IPython extension without modifying your ipython_config.py 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.