Skip to main content

How to Load a .env File in Python with python-dotenv

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

API keys and other settings belong in a .env file, not in your code. The python-dotenv package reads that file and puts each value into os.environ, where your code reads it like any other environment variable. This guide loads one value with python-dotenv 1.2.4 on Python 3.15.2.

Syntax​

Syntax
from dotenv import load_dotenv

load_dotenv(override=<override>)

Each line of the .env file holds one variable:

.env
<NAME>=<value>
PartWhat it means
from dotenv import load_dotenvImports the function. The package you install is called python-dotenv, but the module you import is dotenv
load_dotenv()Finds the .env file and adds each of its variables to os.environ
override=<override>Optional. False, the default, keeps a variable that's already set in the environment. True replaces it with the file's value
<NAME>=<value>One variable in the .env file: its name, an equals sign and its value

The python-dotenv README says that by default load_dotenv() will "Look for a .env file in the same directory as the Python script (or higher up the directory tree)", "Read each key-value pair and add it to os.environ", and "Not override existing environment variables (override=False)."

The Example​

A .env file holds one variable, GREETING. A short script loads the file and prints the variable. Then the same variable is set in the terminal as well, to see which of the two values the script gets.

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: Add python-dotenv to the Project​

Terminal
uv add python-dotenv
Output
Resolved 2 packages in 476ms
Installed 1 package in 29ms
+ python-dotenv==1.2.4

uv add installs the package into the project's environment and adds it to the dependencies list in pyproject.toml.

Step 2: Create the .env File​

Save this as .env in the project folder:

.env
GREETING=Hello from the .env file

The value has spaces and no quotes, and python-dotenv reads it whole: its README says values "can be unquoted, single- or double-quoted".

A .env file is where secrets such as API keys usually go, so add .env to your .gitignore.

Step 3: Write the Script​

Save this as greet.py in the same folder:

greet.py
import os

from dotenv import load_dotenv

load_dotenv()

print(os.environ["GREETING"])

The greet.py script in a macOS-style code window: it imports os and load_dotenv, calls load_dotenv(), then prints the GREETING environment variable.

load_dotenv() runs first, so GREETING is already in os.environ when the last line looks it up. After that, os.environ["GREETING"] reads it like any other environment variable.

Step 4: Run It​

Terminal
uv run python greet.py
Output
Hello from the .env file

The terminal after uv run python greet.py: it prints Hello from the .env file.

The value came from the file. The command works unchanged in PowerShell.

Step 5: Set the Same Variable in the Terminal​

Run the script again with GREETING also set in the terminal:

Terminal
GREETING="Hi from the shell" uv run python greet.py
Output
Hi from the shell

The terminal's value won. load_dotenv() found GREETING already set, so it left it alone and skipped the file's value. The same happens with any variable, an API key included: if your terminal already has one with the same name, that's the one your code gets.

Step 6: Let the File Win with override=True​

Save a copy of the script as greet_override.py, with one change: override=True.

greet_override.py
import os

from dotenv import load_dotenv

load_dotenv(override=True)

print(os.environ["GREETING"])

Run it with the terminal's value still set:

Terminal
GREETING="Hi from the shell" uv run python greet_override.py
Output
Hello from the .env file

Two runs in a macOS terminal with GREETING set to Hi from the shell: greet.py prints Hi from the shell, and greet_override.py prints Hello from the .env file.

With override=True, the file's value replaced the terminal's.

On macOS and Linux, GREETING="…" in front of a command sets the variable for that one command only. In PowerShell, $env:GREETING stays set for the rest of the session, until you close the window. To remove it sooner, run:

Terminal
Remove-Item Env:GREETING

Common Mistakes​

MistakeWhat happensDo this instead
No .env file in the script's folder or any folder above itload_dotenv() loads nothing and raises no error. The script fails later, on the line that reads the variable: KeyError: 'GREETING'Put .env next to the script, or in a folder above it
Expecting the file to beat a variable set in the terminalThe terminal's value is usedRemove the terminal's variable, or pass override=True
Committing .env to GitYour secrets end up in the repositoryAdd .env to .gitignore

FAQ​

Where does load_dotenv() look for the .env file?​

In the script's own folder or, if there's none there, in the folders above it. That's the default the README describes.

Do values with spaces need quotes?​

No. GREETING=Hello from the .env file loaded whole without them. Single or double quotes work too.

Is the package called dotenv or python-dotenv?​

You install python-dotenv and import dotenv: uv add python-dotenv, then from dotenv import load_dotenv.

Conclusion​

load_dotenv() read GREETING from .env into os.environ, and the script printed Hello from the .env file. When the terminal already had a GREETING, that value won, until override=True let the file replace it.

Additional Resources​