Skip to content

Instantly share code, notes, and snippets.

@hngkr hngkr/setenviron.py
Created Jul 12, 2019

Embed
What would you like to do?
setenviron contextmanager source code
import contextlib
import os
@contextlib.contextmanager
def setenviron(envdict=None, **mapping):
"""``with`` context to temporarily modify the environment variables"""
_environ = os.environ.copy()
if envdict:
for key, value in envdict.items():
os.environ[key] = str(value)
for key, value in mapping.items():
os.environ[key] = str(value)
try:
yield
finally:
os.environ.clear()
os.environ.update(_environ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.