philiprehberger-temp-env
Temporarily set, override, or remove environment variables with a context manager.
Installation
pip install philiprehberger-temp-env
Usage
Context manager
from philiprehberger_temp_env import temp_env
with temp_env(API_KEY="test-key", DEBUG="1"):
# API_KEY and DEBUG are set
...
# Original values are restored
Remove a variable temporarily
from philiprehberger_temp_env import temp_env
with temp_env(SECRET=None):
# SECRET is removed from the environment
...
# SECRET is restored
Decorator for tests
from philiprehberger_temp_env import env_override
@env_override(DATABASE_URL="sqlite:///:memory:", DEBUG="0")
def test_database():
...
Load from .env file
from philiprehberger_temp_env import TempEnv
ctx = TempEnv.from_file(".env")
with ctx:
...
TempEnv class directly
from philiprehberger_temp_env import TempEnv
with TempEnv(API_KEY="test", VERBOSE="1"):
...
Unsetting and snapshotting
from philiprehberger_temp_env import temp_unset, snapshot_env, restore_env
# Temporarily remove env vars; restore on exit
with temp_unset("AWS_PROFILE", "AWS_REGION"):
# AWS_PROFILE and AWS_REGION are unset
...
# Explicit save/restore beyond a context manager block
snap = snapshot_env("API_KEY", "DEBUG")
# ... mutate the environment freely ...
restore_env(snap) # Variables that were unset before are removed again
API
| Name | Description |
|---|---|
temp_env(**kwargs: str | None) |
Context manager that temporarily sets, overrides, or removes env vars. Pass None to remove a variable. |
env_override(**kwargs: str | None) |
Decorator that wraps a function with temporary env var overrides. |
TempEnv(**kwargs: str | None) |
Class that can be used as a context manager directly. |
TempEnv.from_file(path) |
Classmethod that parses a .env file and returns a TempEnv instance. |
temp_unset(*names) |
Context manager that temporarily removes the named env vars and restores them on exit. |
snapshot_env(*names) |
Returns a dict[str, str | None] capturing the current value (or None if unset) of each name. |
restore_env(snapshot) |
Restores env vars from a snapshot dict; None values mean "remove if present". |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Release files for philiprehberger-temp-env 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| philiprehberger_temp_env-0.2.0.tar.gz | 188.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| philiprehberger_temp_env-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 194.0 kB
Release files / philiprehberger_temp_env-0.2.0.tar.gz
| Download URL | philiprehberger_temp_env-0.2.0.tar.gz |
|---|---|
| Size | 188.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8227e99020941d061c4f40f6aec843fdbf695f417b86f506c7457df62b99de1b
|
|
BLAKE2b-256 checksum How to use checksums |
c239d296f9ac2acdc3893c85ee40d8bb76f517c8965a0ab6bf8f9c6d835f3687
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_temp_env-0.2.0-py3-none-any.whl
| Download URL | philiprehberger_temp_env-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8d854abb13a2fa04cf8aaf30c25e4561c085c2fb5056c5963ca9497ab6bd0785
|
|
BLAKE2b-256 checksum How to use checksums |
f986256a0fbb8c5e1486a927a356c4aad5cdb6318f5e5d7b05953297f73e1eaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|