Python API[path])

To use an envdir in a Python file (e.g. Django’s simply call the open function of the envdir module:

import envdir

envdir will try to find an envdir directory next to the file you modified.

It’s also possible to explicitly pass the path to the envdir:

import envdir'/home/jezdez/mysite/envs/prod')

Calling open will automatically apply all environment variables to the current instance of os.environ.

If you want to implement more advanced access to envdirs there is also an own dict-like Env object to work with. The above example could also be written like this:

import envdir

env ='/home/jezdez/mysite/envs/prod')

The returned Env instance has a dict-like interface but also features a clear() method to reset the current instance of os.environ to the value it had before the envdir was opened:

import envdir

env ='/home/jezdez/mysite/envs/prod')

# do something


Since calling the clear method should be done in a transparent manner you can also use it as a context manager:

import envdir

with'/home/jezdez/mysite/envs/prod') as env:
    # do something

Outside the context manager block the environ is reset back automatically.

To access and write values you can also use the dict-like interface:

import envdir

with as env:
    env['DATABASE_URL'] = 'sqlite://:memory:'
    assert 'DATABASE_URL' in env
    assert env.items() == [('DATABASE_URL', 'sqlite://:memory:')]


Additions to the envdir done inside the context manager block are persisted to disk and will be available the next time your open the envdir again.

Of course you can also directly interact with Env instances, e.g.:

import envdir

with envdir.Env('/home/jezdez/mysite/envs/prod') as env:
    # do something here

The difference between instantiating an Env yourself to using is that you’ll lose the automatic discovery of the envdir directory.

See the API docs below for a full list of methods available in the Env object.

class envdir.Env(path)[source]

An dict-like object to represent an envdir environment with extensive API, can be used as context manager, too.

__exit__(type, value, traceback)[source]
__hash__ = None
__module__ = 'envdir.env'
__setitem__(name, value)[source]

Clears the envdir by resetting the os.environ items to the values it had before opening this envdir (or removing them if they didn’t exist). Doesn’t delete the envdir files.

fromkeys(iterable, value=None)
get(key, failobj=None)
pop(key, *args)
setdefault(key, failobj=None)
update(dict=None, **kwargs)