Temporarily set, override, or remove environment variables with a context manager
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_temp_env-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_temp_env-0.2.0.tar.gz
- Upload date:
- Size: 188.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8227e99020941d061c4f40f6aec843fdbf695f417b86f506c7457df62b99de1b
|
|
| MD5 |
dbad89399fd5c2b55749e01ae46876d2
|
|
| BLAKE2b-256 |
c239d296f9ac2acdc3893c85ee40d8bb76f517c8965a0ab6bf8f9c6d835f3687
|
File details
Details for the file philiprehberger_temp_env-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_temp_env-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d854abb13a2fa04cf8aaf30c25e4561c085c2fb5056c5963ca9497ab6bd0785
|
|
| MD5 |
bc63414611449e55b00805a695ffe289
|
|
| BLAKE2b-256 |
f986256a0fbb8c5e1486a927a356c4aad5cdb6318f5e5d7b05953297f73e1eaa
|