Skip to content

Context manager to update environment variables with preservation

License

Notifications You must be signed in to change notification settings

sakurai-youhei/envcontext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

envcontext

Context manager to update environment variables with preservation

Build status

Installation

pip install envcontext

Usage

Ex. 1: Update environment variables in current process

from os import environ, getenv
from envcontext import EnvironmentContext as EnvContext

environ["TEST_VAR1"] = "original"

with EnvContext(TEST_VAR1="updated", TEST_VAR2="added"):
    print(environ["TEST_VAR1"])  # Prints "updated".
    print(environ["TEST_VAR2"])  # Prints "added".

print(getenv("TEST_VAR1"))  # Prints "original".
print(getenv("TEST_VAR2"))  # Should print "None".

Ex. 2: Update environment variables in child process

from subprocess import check_output
from envcontext import EnvironmentContext as EnvContext

with EnvContext(PGPASSWORD="very-secret-password"):
    check_output(["psql", "..."])  # psql process can manipulate PGPASSWORD.

Note: Ex. 2 is not working on Python 2.x because subprocess module bundled in Python 2.x doesn't propagate environment variables from current to child process.

About

Context manager to update environment variables with preservation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages