{"id":10283,"library":"tempenv","title":"Temporary Environment Variable Context Manager","description":"tempenv is a lightweight Python context manager and decorator for temporarily setting and unsetting environment variables. It guarantees that environment variables are restored to their original state after the context exits or a decorated function completes, making it ideal for testing or local development. The current version is 2.1.0, with releases primarily driven by Python version compatibility updates and minor maintenance.","status":"active","version":"2.1.0","language":"en","source_language":"en","source_url":"https://github.com/jeking3/tempenv","tags":["environment variables","context manager","decorator","testing","utility","os.environ"],"install":[{"cmd":"pip install tempenv","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"TemporaryEnvironment","correct":"from tempenv import TemporaryEnvironment"}],"quickstart":{"code":"import os\nfrom tempenv import TemporaryEnvironment\n\n# --- Usage as a Context Manager ---\n# Get current value for example\noriginal_path = os.environ.get('PATH')\nprint(f\"Initial PATH (might be long, truncated): {original_path[:50]}...\")\n\nwith TemporaryEnvironment(PATH='/tmp/test_path'):\n    # Inside this block, PATH is temporarily changed\n    current_path = os.environ.get('PATH')\n    print(f\"Inside context: PATH={current_path}\")\n    assert current_path == '/tmp/test_path'\n\n# Outside the block, PATH is restored to its original value\nrestored_path = os.environ.get('PATH')\nprint(f\"After context: PATH (truncated): {restored_path[:50]}...\")\nassert restored_path == original_path\n\n# --- Usage as a Decorator ---\n@TemporaryEnvironment(MY_API_KEY='temp_key_123')\ndef fetch_data_with_temp_key():\n    print(f\"\\nInside decorated function: MY_API_KEY={os.environ.get('MY_API_KEY')}\")\n    assert os.environ.get('MY_API_KEY') == 'temp_key_123'\n\nprint(f\"Before decorated call: MY_API_KEY={os.environ.get('MY_API_KEY')}\")\nfetch_data_with_temp_key()\nprint(f\"After decorated call: MY_API_KEY={os.environ.get('MY_API_KEY')}\")\n# MY_API_KEY is unset or restored after the function returns\nassert os.environ.get('MY_API_KEY') is None\n","lang":"python","description":"This example demonstrates how to use `TemporaryEnvironment` as both a context manager and a decorator to temporarily set and restore environment variables. It's useful for isolating tests or managing environment-specific configurations."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or higher, or pin `tempenv` to a compatible older version (e.g., `tempenv<2.0.0` for Python 3.6, or `tempenv<2.1.0` for Python 3.7/3.8).","message":"Dropped support for older Python versions. `tempenv>=2.0.0` requires Python 3.7+ (dropped 3.6). `tempenv>=2.1.0` further dropped Python 3.7 and 3.8, now requiring Python 3.9+.","severity":"breaking","affected_versions":">=2.0.0, >=2.1.0"},{"fix":"Test existing code thoroughly after upgrading to v2.0.0+, particularly if using `TemporaryEnvironment` in complex, interleaved context manager or decorator patterns.","message":"The internal implementation of `TemporaryEnvironment` changed to use `ContextDecorator` in v2.0.0. While the public API remains largely compatible, subtle behavioral differences might arise in highly complex or deeply nested scenarios if previous reliance was on specific pre-2.0.0 internals.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure all `TemporaryEnvironment` initializations include at least one `key=value` pair. Upgrade to v2.0.0 or later for more flexible initialization that allows creating an empty context (e.g., `with TemporaryEnvironment():`).","message":"Prior to v2.0.0, the `TemporaryEnvironment` initializer required at least one `key=value` pair. Attempting to initialize it without any arguments (e.g., `TemporaryEnvironment()`) would raise a `TypeError`.","severity":"gotcha","affected_versions":"<2.0.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Provide at least one `key=value` pair, e.g., `with TemporaryEnvironment(FOO='bar'):`, or upgrade to `tempenv>=2.0.0` which allows empty initialization using `with TemporaryEnvironment():`.","cause":"Attempting to initialize `TemporaryEnvironment()` without any arguments on `tempenv` versions prior to 2.0.0. The constructor historically required at least one environment variable to be set.","error":"TypeError: TemporaryEnvironment.__init__() missing 1 required positional argument: 'value'"},{"fix":"Unpack the dictionary using the `**` operator: `with TemporaryEnvironment(**{'MY_VAR': 'value'}):` or pass variables as individual keyword arguments: `with TemporaryEnvironment(MY_VAR='value'):`.","cause":"Trying to pass a dictionary of environment variables directly as a positional argument (e.g., `TemporaryEnvironment({'MY_VAR': 'value'})`) instead of using keyword arguments or unpacking the dictionary.","error":"TypeError: TemporaryEnvironment.__init__() takes 1 positional argument but 2 were given"}]}