How to Load a .env File in Python with python-dotenv
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
from dotenv import load_dotenv
load_dotenv(override=<override>)
Each line of the .env file holds one variable:
<NAME>=<value>
| Part | What it means |
|---|---|
from dotenv import load_dotenv | Imports 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
uv add python-dotenv
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:
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:
import os
from dotenv import load_dotenv
load_dotenv()
print(os.environ["GREETING"])

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
uv run python greet.py
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:
- macOS / Linux
- Windows (PowerShell)
GREETING="Hi from the shell" uv run python greet.py
$env:GREETING = "Hi from the shell"; uv run python greet.py
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.
import os
from dotenv import load_dotenv
load_dotenv(override=True)
print(os.environ["GREETING"])
Run it with the terminal's value still set:
- macOS / Linux
- Windows (PowerShell)
GREETING="Hi from the shell" uv run python greet_override.py
$env:GREETING = "Hi from the shell"; uv run python greet_override.py
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:
Remove-Item Env:GREETING
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No .env file in the script's folder or any folder above it | load_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 terminal | The terminal's value is used | Remove the terminal's variable, or pass override=True |
Committing .env to Git | Your secrets end up in the repository | Add .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
- python-dotenv — the README, with
load_dotenv(), its options and the.envfile format. - python-dotenv on PyPI — releases and install instructions.
- os.environ — Python's mapping of environment variables.
