Skip to main content

How to Read an Environment Variable in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change from one computer to the next, such as a port, a database address or an API key, belong in environment variables. Python reads them from os.environ, and os.environ.get returns None instead of failing when a variable isn't set.

Syntax​

Syntax
import os

<value> = os.environ.get("<NAME>", <default>)
PartWhat it does
os.environHolds the environment variables the program started with.
.get("<NAME>")Returns the variable's value as a string, or None when it isn't set.
<default>Optional. Returned instead of None when the variable isn't set.

The os documentation describes os.environ as "A mapping object where keys and values are strings that represent the process environment."

The Example​

The example prints the value of a variable named GREETING, once with the variable set and once without it.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which imports os, reads os.environ.get("GREETING") into greeting and prints GREETING is: followed by its value.

main.py
import os

greeting = os.environ.get("GREETING")

print(f"GREETING is: {greeting}")

Step 2: Set the Variable and Run It​

Set GREETING to Hello and run the script. Then remove the variable and run it again:

Terminal
export GREETING=Hello
uv run python main.py
unset GREETING
uv run python main.py

A zsh terminal where export GREETING=Hello and uv run python main.py print GREETING is: Hello, and after unset GREETING the same command prints GREETING is: None.

Output
GREETING is: Hello
GREETING is: None

With the variable set, os.environ.get returned 'Hello'. Without it, the same call returned None and the script carried on, so check for None when the value is required.

A Default Value, or an Error​

Pass a second argument for the value to use when the variable is missing: os.environ.get("GREETING", "Hi") gives 'Hi'. Square brackets work the other way round: os.environ["GREETING"] stops the script with a KeyError when the variable isn't set, which suits settings the program can't run without.

Common Mistakes​

MistakeWhat happensDo this instead
Using a number from the environment directlyIt's a string: PORT=3000 gives '3000'Convert it with int(...)
Setting the variable in one terminal and running Python in anotherNone: each terminal has its own variablesSet it in the terminal that runs the script
Reading a required setting with .get and not checking itThe script carries on with None and fails laterUse os.environ["NAME"], or check for None

Troubleshooting​

SymptomLikely causeFix
The value is NoneThe variable isn't set in this terminalSet it as in Step 2
KeyError: 'GREETING'os.environ["GREETING"] read a missing variableSet the variable, or use .get with a default

FAQ​

What's the difference between os.getenv and os.environ.get?​

For reading, they behave the same: both return None, or your default, when the variable is missing.

Can I change an environment variable from Python?​

Yes, by assigning to os.environ["NAME"]. The change applies to this script and the programs it starts, not to the terminal that launched it.

Conclusion​

os.environ.get("NAME") reads an environment variable as a string and returns None when it's missing; add a default as the second argument. Use os.environ["NAME"] when the script must stop without it.

Additional Resources​