Registry / testing / tempenv

tempenv

JSON →
library2.1.0pypypi✓ verified 85d ago

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 tempenv
INSTALL
IMPORT
SIG · TEMPENV
T
tempenv
testingpythonv2.1.0
Install
1.5s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.008s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TemporaryEnvironment
from tempenv import TemporaryEnvironment

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.

import os from tempenv import TemporaryEnvironment # --- Usage as a Context Manager --- # Get current value for example original_path = os.environ.get('PATH') print(f"Initial PATH (might be long, truncated): {original_path[:50]}...") with TemporaryEnvironment(PATH='/tmp/test_path'): # Inside this block, PATH is temporarily changed current_path = os.environ.get('PATH') print(f"Inside context: PATH={current_path}") assert current_path == '/tmp/test_path' # Outside the block, PATH is restored to its original value restored_path = os.environ.get('PATH') print(f"After context: PATH (truncated): {restored_path[:50]}...") assert restored_path == original_path # --- Usage as a Decorator --- @TemporaryEnvironment(MY_API_KEY='temp_key_123') def fetch_data_with_temp_key(): print(f"\nInside decorated function: MY_API_KEY={os.environ.get('MY_API_KEY')}") assert os.environ.get('MY_API_KEY') == 'temp_key_123' print(f"Before decorated call: MY_API_KEY={os.environ.get('MY_API_KEY')}") fetch_data_with_temp_key() print(f"After decorated call: MY_API_KEY={os.environ.get('MY_API_KEY')}") # MY_API_KEY is unset or restored after the function returns assert os.environ.get('MY_API_KEY') is None
Debug
Known issues
breakingDropped support for older Python versions. `tempenv>=2.0.0` requires Python 3.7+ (dropped 3.6). `tempenv>=2.1.0` further dropped Python 3.7 and 3.8, now requiring Python 3.9+.
fix
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).
affects: >=2.0.0, >=2.1.0
breakingThe internal implementation of `TemporaryEnvironment` changed to use `ContextDecorator` in v2.0.0. While the public API remains largely compatible, subtle behavioral differences might arise in highly complex or deeply nested scenarios if previous reliance was on specific pre-2.0.0 internals.
fix
Test existing code thoroughly after upgrading to v2.0.0+, particularly if using `TemporaryEnvironment` in complex, interleaved context manager or decorator patterns.
affects: >=2.0.0
gotchaPrior to v2.0.0, the `TemporaryEnvironment` initializer required at least one `key=value` pair. Attempting to initialize it without any arguments (e.g., `TemporaryEnvironment()`) would raise a `TypeError`.
fix
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():`).
affects: <2.0.0
Errors
Common errors & fixes
TypeError: TemporaryEnvironment.__init__() missing 1 required positional argument: 'value'
Attempting to initialize `TemporaryEnvironment()` without any arguments on `tempenv` versions prior to 2.0.0. The constructor historically required at least one environment variable to be set.
fix
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():`.
TypeError: TemporaryEnvironment.__init__() takes 1 positional argument but 2 were given
Trying to pass a dictionary of environment variables directly as a positional argument (e.g., `TemporaryEnvironment({'MY_VAR': 'value'})`) instead of using keyword arguments or unpacking the dictionary.
fix
Unpack the dictionary using the `**` operator: `with TemporaryEnvironment(**{'MY_VAR': 'value'}):` or pass variables as individual keyword arguments: `with TemporaryEnvironment(MY_VAR='value'):`.
Upgrade
Version history
2.1.0latest on PyPI · released Mar 23, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
tempenv — pip install tempenv · libregistry