A decorator and context manager for temporarily modifying os.environ
.
# as a context manager
with modified_environ(added={...}, absent=[...]):
...
# as a decorator
@modified_environ(added={...}, absent=[...])
Installation:
$ pip install pollute
modified_environ
modifies os.environ
in-place, ensuring that all
references to it in the code are updated. All changes made by
modified_environ
are reversed when exiting the context or decorator.
import os
from pollute import modified_environ
assert 'HELLO' not in os.environ
assert 'PATH' in os.environ
with modified_environ(added={'HELLO': 'WORLD'}, absent=['PATH']):
assert os.environ['HELLO'] == 'WORLD'
assert 'PATH' not in os.environ
assert 'HELLO' not in os.environ
assert 'PATH' in os.environ