How to Set an Environment Variable in Python with os.environ
A program sometimes needs an environment variable that it sets itself, for example a setting that a library reads when it starts. In Python you set one by assigning to os.environ, the same way you set a key in a dictionary. This guide sets a variable and reads it back on Python 3.15.2.
Syntax
os.environ["<NAME>"] = "<value>"
| Part | What it means |
|---|---|
os.environ | The environment of the running program: every variable's name and value, as a mapping |
<NAME> | The variable's name, as a string. By convention it's written in capital letters |
<value> | The value, which must be a string |
The os documentation describes os.environ as "A mapping object where keys and values are strings that represent the process environment", and adds: "This mapping may be used to modify the environment as well as query the environment."
The Example
A script sets APP_MODE to test, then reads it back two ways: with os.environ and with os.getenv.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import os
os.environ["APP_MODE"] = "test"
print(os.environ["APP_MODE"])
print(os.getenv("APP_MODE"))
![The set_env.py script in a macOS-style code window: it imports os, sets os.environ["APP_MODE"] to "test", then prints os.environ["APP_MODE"] and os.getenv("APP_MODE").](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-os-environ-set-01-code.webp)
import os loads the module that holds os.environ. The assignment sets the variable, and the two print lines read it back.
Step 2: Run It
uv run python set_env.py
test
test

Both lines print test. os.getenv reads the same mapping as os.environ, so it sees the new value too. The command works unchanged in PowerShell.
Step 3: Check the Terminal Afterwards
Run the script again, then print the variable in the terminal that ran it. The square brackets show where an empty value would be:
- macOS / Linux
- Windows (PowerShell)
uv run python set_env.py
echo "[$APP_MODE]"
uv run python set_env.py
"[$env:APP_MODE]"
test
test
[]

The terminal's APP_MODE is empty. A program's environment belongs to that program: setting a variable changes it for the program and for the programs it starts afterwards, as the os.putenv documentation says: "Such changes to the environment affect subprocesses started with os.system(), popen() or fork() and execv()." It never reaches back to the terminal that started it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Assigning a number: os.environ["PORT"] = 8080 | TypeError: str expected, not int | Pass a string: os.environ["PORT"] = "8080", and convert it back with int() where you read it |
| Expecting the terminal to see the variable after the script ends | It doesn't: the terminal printed [] in Step 3 | Set it in the terminal itself, or in the program that needs it |
FAQ
Should I use os.putenv instead?
No. The os documentation explains: "Assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don't update os.environ, so it is actually preferable to assign to items of os.environ."
What if the variable is already set?
The assignment replaces its value for this program. The terminal's own value stays as it was.
Conclusion
os.environ["APP_MODE"] = "test" set the variable for the running program: os.environ and os.getenv both read test. The terminal that started the script still had no APP_MODE afterwards, because a program only changes its own environment and the environment of the programs it starts.
Additional Resources
- os.environ — the mapping and what changing it does.
- os.getenv — reading a variable with a default.
- os.putenv — the function
os.environcalls for you.
