Bases: object
A context manager for managing environment variables
This class provides a simplified interface for consistently setting
and restoring environment variables, with special handling to ensure
consistency with the C Runtime Library environment on Windows
platforms.
os.environ reflects the current python environment variables, and
will be passed to subprocesses. However, it does not reflect the C
Runtime Library (MSVCRT) environment on Windows platforms. This can
be problemmatic as DLLs loaded through the CTYPES interface will see
the MSVCRT environment and not os.environ. This class provides a
way to manage environment variables and pass changes to both
os.environ and the MSVCRT runtime.
This class implements a context manager API, so that clients can
temporarily change - and then subsequently restore - the
environment.
>>> os.environ['TEMP_ENV_VAR'] = 'original value'
>>> print(os.environ['TEMP_ENV_VAR'])
original value
>>> with CtypesEnviron(TEMP_ENV_VAR='temporary value'):
... print(os.environ['TEMP_ENV_VAR'])
temporary value
>>> print(os.environ['TEMP_ENV_VAR'])
original value
-
__init__(**kwds)[source]
Methods
Attributes
Member Documentation
-
restore()[source]
Restore the environment to the original state
This restores all environment variables modified through this
object to the state they were in before this instance made any
changes. Note that any changes made directly to os.environ
outside this instance will not be detected / undone.