Python API

envdir.open([path])

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

import envdir
envdir.open()

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

envdir.open('/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 = envdir.open('/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 = envdir.open('/home/jezdez/mysite/envs/prod')

# do something

env.clear()

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

import envdir

with envdir.open('/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 envdir.open() as env:
    env['DATABASE_URL'] = 'sqlite://:memory:'
    assert 'DATABASE_URL' in env
    assert env.items() == [('DATABASE_URL', 'sqlite://:memory:')]

Note

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 envdir.open() 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.

__cmp__(dict)
__contains__(name)[source]
__delitem__(name)[source]
__enter__()[source]
__exit__(type, value, traceback)[source]
__getitem__(name, default=<object object>)[source]
__hash__ = None
__init__(path)[source]
__iter__()
__len__()
__module__ = 'envdir.env'
__repr__()[source]
__setitem__(name, value)[source]
clear()[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.

copy()
fromkeys(iterable, value=None)
get(key, failobj=None)
has_key(key)
items()
iteritems()
iterkeys()
itervalues()
keys()
pop(key, *args)
popitem()
setdefault(key, failobj=None)
update(*args, **kwargs)
values()