Skip to main content

Temporarily set, override, or remove environment variables with a context manager

Project description

philiprehberger-temp-env

Tests PyPI version Last updated

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"):
    ...

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.

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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

philiprehberger_temp_env-0.1.8.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_temp_env-0.1.8-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_temp_env-0.1.8.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_temp_env-0.1.8.tar.gz
Algorithm Hash digest
SHA256 4d4ad1b36b6cdbc371a8d34f7089deedce0872cdbd8ae430937f760f4007e171
MD5 c45fde41d0db55e330c860b86ce97caf
BLAKE2b-256 f3e3ed74182f34015d09ea1db6fbb6e374070175c4ac90ad4e7d057e3caf9222

See more details on using hashes here.

File details

Details for the file philiprehberger_temp_env-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_temp_env-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 777bf3b25a07419bfa0711cbcc9e7180a3f0d0d3d8cd28888c3ed281334d0060
MD5 f8f3dbc7bc1306d86df2cea8f2f935a8
BLAKE2b-256 6dc63a5b09479305ebbb8670aa78f344f97573348a32c399285dbb0bdd533975

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page