setenvironment

raw JSON →
2.0.3 verified Fri May 01 auth: no python

A cross-platform library for setting environment variables in the current process and launching subprocesses with modified environments. Version 2.0.3 requires Python >=3.7 and is hosted on GitHub by zackees. Release cadence is sporadic.

pip install setenvironment
error AttributeError: module 'setenvironment' has no attribute 'set_env'
cause Installed an old version (1.x) where the function was named differently (e.g., `setenv` or `export_env`).
fix
Upgrade to version 2.0.3: pip install --upgrade setenvironment and use from setenvironment import set_env.
error TypeError: set_env() takes 2 positional arguments but 3 were given
cause Using `set_env('KEY', 'value1', 'value2')` or calling with keyword arguments like `set_env(name='key', value='val')`.
fix
Use set_env('KEY', 'value') with exactly two positional arguments.
error ModuleNotFoundError: No module named 'setenvironment'
cause Library not installed or installed in a different Python environment.
fix
Run pip install setenvironment in the correct environment.
gotcha `set_env` modifies the current process environment (os.environ). It does NOT affect the parent shell or persist after Python exits.
fix Use `run_with_env` to spawn subprocesses with desired environment, or rely on `os.environ` modifications within the same Python session.
deprecated In version 1.x, `set_env` accepted keyword arguments like `name=value`. In 2.x, only positional arguments are supported.
fix Use `set_env('NAME', 'value')` instead of `set_env(NAME='value')`.
breaking In version 2.0.0, the function `export_env` was removed. Use `set_env` instead.
fix Replace `export_env(...)` with `set_env(...)`.

Set an environment variable in the current process, then run a subprocess with an overridden environment.

from setenvironment import set_env, run_with_env

set_env('MY_VAR', 'hello')
run_with_env(['echo', '$MY_VAR'], {'MY_VAR': 'world'})