I use the following code to temporarily modify environment variables.
@contextmanager
def _setenv(**mapping):
\"\"\"``with`` context to temporarily modi
Using the gist here, you can save/restore local, global scope variable and environment variables: https://gist.github.com/earonesty/ac0617a5672ae1a41be1eaf316dd63e4
import os
from varlib import vartemp, envtemp
x = 3
y = 4
with vartemp({'x':93,'y':94}):
print(x)
print(y)
print(x)
print(y)
with envtemp({'foo':'bar'}):
print(os.getenv('foo'))
print(os.getenv('foo'))
This outputs:
93
94
3
4
bar
None