tempenv
Manage environment variables in a temporary scope.
Some products use environment variables as a primary means to supply credentials. To ensure the lifetime of exposed credentials is short, wrap them in a TemporaryEnvironment so that they are automatically destroyed on scope exit.
You can:
- Set or unset environment variables inside a
withcode block, - Get a warning if the code block modifies one of the environment variables,
- Optionally bypass restoration of the original environment variable value if the code block modifies the environment variable.
Install
Install the latest version of tempenv:
pip install tempenv
Examples
Each of these examples can be found in the tests.
Set some environment variables temporarily: (see tests/example_set_test.py):
def test_set(self):
user_before = os.environ.get("USER")
with TemporaryEnvironment({"USER": "nobody", "OTHER": "foo"}):
assert os.environ.get("USER") == "nobody"
assert os.environ.get("OTHER") == "foo"
assert os.environ.get("USER") == user_before
Changing the value to None will unset the environment variable during
the code block
(see tests/example_unset_test.py):
def test_unset(self):
os.environ["DEBUG"] = "1"
with TemporaryEnvironment({"DEBUG": None}):
assert "DEBUG" not in os.environ
assert "DEBUG" in os.environ
Changing a temporary environment variable during the scope will cause a warning (see tests/example_overwrite_test.py):
def test_overwritten_in_context(self):
with self.assertWarnsRegex(EnvironmentVariableChangedWarning, "FOO"):
with TemporaryEnvironment({"FOO": "BAR"}):
os.environ["FOO"] = "SAM"
If you set the optional argument restore_if_changed=False then a change
during the scope of the TemporaryEnvironment will not issue a warning and will
not restore to the original value
(see tests/example_ignore_test.py):
def test_ignored_overwrite_in_context(self):
os.environ["FOO"] = "BAR"
with TemporaryEnvironment({"FOO": "SAM"}, restore_if_changed=False):
os.environ["FOO"] = "DEAN"
assert os.environ["FOO"] == "DEAN"
You can use TemporaryEnvironment in a unittest scope as follows (see tests/example_unittest_test.py):
@TemporaryEnvironment({"USER": "Crowley"})
def test_check(self):
assert os.environ.get("USER") == "Crowley"
License
Released under the Apache Software License, Version 2.0 (see LICENSE):
Copyright (C) 2019 - 2022 James E. King III (@jeking3) <jking@apache.org>
Bugs
Please report any bugs that you find on GitHub.
Or, even better, fork the repository on GitHub
and create a pull request (PR). We welcome all changes, big or small, and we
will help you make the PR if you are new to git (just ask on the issue and/or
see CONTRIBUTING.rst).
Metadata
Release files for tempenv 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tempenv-2.1.0.tar.gz | 10.4 kB | Without provenance | Details for tempenv-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tempenv-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for tempenv-2.1.0-py3-none-any.whl |
Total release size: 19.9 kB
Release files / tempenv-2.1.0.tar.gz
| Download URL | tempenv-2.1.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
45439419d9ff63a338af323f01d69fa20627bab94f37605ef7f47b7bc7f17b96
|
|
BLAKE2b-256 checksum How to use checksums |
7aafac55e665dafa99311d1397b1a28c0ce289c987e3afbbd24b7a18a3084bbc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / tempenv-2.1.0-py3-none-any.whl
| Download URL | tempenv-2.1.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb19bec4f516e551cd0ce33b1d69993261986cfd503d09a4f2201560f9c31a4e
|
|
BLAKE2b-256 checksum How to use checksums |
d08141ae0ee580b5436f9e9c39c537aed7cf37fc47f5fd07fe574af254235259
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Security for tempenv 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.