tempenv is a lightweight Python context manager and decorator for temporarily setting and unsetting environment variables. It guarantees that environment variables are restored to their original state after the context exits or a decorated function completes, making it ideal for testing or local development. The current version is 2.1.0, with releases primarily driven by Python version compatibility updates and minor maintenance.
pip install tempenvVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `TemporaryEnvironment` as both a context manager and a decorator to temporarily set and restore environment variables. It's useful for isolating tests or managing environment-specific configurations.
Upgrade your Python environment to 3.9 or higher, or pin `tempenv` to a compatible older version (e.g., `tempenv<2.0.0` for Python 3.6, or `tempenv<2.1.0` for Python 3.7/3.8).
Test existing code thoroughly after upgrading to v2.0.0+, particularly if using `TemporaryEnvironment` in complex, interleaved context manager or decorator patterns.
Ensure all `TemporaryEnvironment` initializations include at least one `key=value` pair. Upgrade to v2.0.0 or later for more flexible initialization that allows creating an empty context (e.g., `with TemporaryEnvironment():`).
Provide at least one `key=value` pair, e.g., `with TemporaryEnvironment(FOO='bar'):`, or upgrade to `tempenv>=2.0.0` which allows empty initialization using `with TemporaryEnvironment():`.
Unpack the dictionary using the `**` operator: `with TemporaryEnvironment(**{'MY_VAR': 'value'}):` or pass variables as individual keyword arguments: `with TemporaryEnvironment(MY_VAR='value'):`.No dependency data recorded yet.