Skip to main content

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

Project description

philiprehberger-temp-env

Tests PyPI version License

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.

Development

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

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.5.tar.gz (3.7 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.5-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_temp_env-0.1.5.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_temp_env-0.1.5.tar.gz
Algorithm Hash digest
SHA256 1bc1fc5ff616c323e381751060aab64f90f48ebf44b618164ab740c17b2b3a94
MD5 e1ac344e91d168cf9c1f2e1a620a333a
BLAKE2b-256 df11f8f8044408d3b7fafb131fac5c357102cb019123bd79461039d31dcf8450

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_temp_env-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4d39811aa41885a224b4e1e6248532c8073c511bea0f6c3e1f1df7b0bc3ec36c
MD5 208ea0d87c8f2c0f1ebc0f4b21dbcaba
BLAKE2b-256 bed58882ecccc188a864eb67bb52e70cff3c952652c7d3a138315f31e0ff19a7

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