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.
Install
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"):
...
API Reference
| 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. |
License
MIT
Project details
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.1.2.tar.gz.
File metadata
- Download URL: philiprehberger_temp_env-0.1.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f2a9a3897e6f12a448748aea2a753c0afb48d8eeab4d22107a189f4c09666eb
|
|
| MD5 |
bc2e750b20527692333a2c758d50900a
|
|
| BLAKE2b-256 |
2dd23a3b7e5db749a71a8e932e7d43f129d100800a6402493fdf2f4de2798ebd
|
File details
Details for the file philiprehberger_temp_env-0.1.2-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_temp_env-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
703ab8b2f5c05212cc988c989e7027066ef8022d49cf1e3050a9431f6d039e4d
|
|
| MD5 |
5548df63a4a5bad40356ffaedb68da6e
|
|
| BLAKE2b-256 |
be84dda5b1923418781cc82d645b354250560a9efd3e35431462452d933f0587
|