tempenv
Environment Variable Context Manager
What it is and what it does
tempenv is a context manager for Python that temporarily sets or unsets environment variables and automatically restores them when the block exits. It's designed for scenarios where credentials or other sensitive configuration must be scoped to a short lifetime—common in testing and in applications that receive secrets via environment variables. The package provides a simple `with TemporaryEnvironment({...})` block that accepts a dictionary of variable names to values; setting a value to `None` unsets the variable. It can optionally warn if code inside the block modifies one of the temporary variables, and can be configured to either restore the original value or leave the modified value in place.
The package has no runtime dependencies, installs as a pure Python wheel, and supports Python 3.9 through 3.13. It can be used as a context manager in regular code or as a decorator on test methods. The main use case is isolating credential or configuration changes to a specific code scope, ensuring they don't leak into the broader process environment.
Use it for:
- Wrap credential injection in tests to ensure secrets are only present during the test and automatically cleaned up afterward
- Temporarily override configuration variables for a specific function call without affecting the rest of the application
- Suppress or modify environment variables during unit tests to verify behavior under different configurations
- Decorate test methods to inject test-specific environment variables without manual setup/teardown
- Prevent accidental credential leakage in long-running processes by scoping secrets to the exact code block that uses them
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Temporarily override environment variables within a scoped context, automatically restoring the original state when the block exits.
Yes, if you need scoped environment variable management in tests or credential-sensitive code. The package is stable, has no dependencies, and solves a real problem cleanly. The aging maintenance status (509 days since last release) is not a blocker for a simple, focused tool—check whether the code already handles your Python version and use case before adopting.
Install
tempenv on PyPI
pip
pip install tempenvuv
uv add tempenvpoetry
poetry add tempenvInstalling tempenv
Before you install
Low friction: pure Python wheel with no runtime dependencies. Maintenance is aging—last release was 509 days ago and the repository shows minimal activity (7 stars), though it remains archived=false and the code is marked Production/Stable.
License in practice
Apache License 2.0 is permissive; you can use this package freely in commercial and private projects with minimal restrictions, provided you include a copy of the license.
Quickstart
pip install tempenv
import os
from tempenv import TemporaryEnvironment
with TemporaryEnvironment({"API_KEY": "value"}):
print(os.environ.get("API_KEY"))
# API_KEY is restored to its previous value
Requires Python >=3.9
Verify before relying
- Whether the 509-day gap since last release indicates active maintenance or stable-enough code that updates are rare
- Performance characteristics when managing large numbers of environment variables in nested contexts
Package facts
| License | Apache License 2.0 (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | aging — 509 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 181,869/month — #10,111 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: tempenv-2.1.0-py3-none-any.whl
Keywords: temporary, environment variable, context manager, test, testing
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytest-envA pytest plugin that loads environment…
permissive · top 1,000 on PyPI
envsubstSubstitutes environment variables in strings…
permissive · top 15,000 on PyPI
stubsProvides a context-manager syntax for…
permissive · top 15,000 on PyPI
pytest-shutilProvides pytest fixtures and utilities for…
permissive · top 15,000 on PyPI
puccinialinInstalls Rust and Cargo into a temporary cache…
permissive · top 5,000 on PyPI
django-environLoads and parses environment variables from…
permissive · top 5,000 on PyPI
django-read-onlyPrevents Django database write operations by…
permissive · top 15,000 on PyPI
memory-tempfileIdentifies and provides access to RAM-based…
permissive · top 15,000 on PyPI
acresAcres provides a consistent, scoped interface…
permissive · top 15,000 on PyPI