Skip to main content

How to Set an Environment Variable in Python with os.environ

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
os.environ["<NAME>"] = "<value>"
PartWhat it means
os.environThe 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​

set_env.py
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").

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​

Terminal
uv run python set_env.py
Output
test
test

The terminal after uv run python set_env.py: the word test, printed twice.

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:

Terminal
uv run python set_env.py
echo "[$APP_MODE]"
Output
test
test
[]

The terminal after running set_env.py and then printing APP_MODE in the shell: test, test, then empty square brackets.

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​

MistakeWhat happensDo this instead
Assigning a number: os.environ["PORT"] = 8080TypeError: str expected, not intPass 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 endsIt doesn't: the terminal printed [] in Step 3Set 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.environ calls for you.