skillfed

tempenv

Environment Variable Context Manager

tempenv v2.1.0 181.9K downloads/30d#10,111 on PyPI7
Permissive license Apache License 2.0 AGING released

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 tempenv

uv

uv add tempenv

poetry

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 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.9Topic :: SecurityTopic :: System :: ShellsTopic :: UtilitiesTyping :: Typed

Tags

environment variable context managertemporary env varsscoped environment overridetest environment isolationcredential scope management
testingcredentialscontext-manager

More Utilities packages