--- id: tempenv version: "2.1.0" license: Apache License 2.0 license_treatment: permissive maintenance: aging --- # tempenv — Environment Variable Context Manager License: permissive · Maintenance: aging · Downloads: 181.9K/mo ## 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 above — 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 pip install tempenv uv add tempenv poetry add tempenv ## Installing 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_current - Install friction: low - Maintenance: aging - Downloads: 181.9K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags environment variable context manager, temporary env vars, scoped environment override, test environment isolation, credential scope management, testing, credentials, context-manager [View on SkillFed](https://skillfed.io/packages/tempenv) · [View on PyPI](https://pypi.org/project/tempenv/)