Skip to main content

How to Load a .env File with uv run --env-file

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings such as API keys belong outside your code, and a .env file next to the project is the usual place for them. uv can read that file and put its values into the environment of the program it runs, with no extra package and the same command in every shell. This guide loads one variable that way, with real output from uv 0.13.10.

Syntax​

Syntax
uv run --env-file <file> <command>
PartWhat it means
--env-file <file>Reads NAME=value lines from <file> and sets them for the command. Can be given several times
<command>What to run, such as show_env.py or python script.py

uv's documentation: "uv run can load environment variables from dotenv files (e.g., .env, .env.local, .env.development), powered by the dotenvy crate. To load a .env file from a dedicated location, set the UV_ENV_FILE environment variable, or pass the --env-file flag to uv run." It adds: "If the same variable is defined in the environment and in a .env file, the value from the environment will take precedence."

Prerequisites​

  • uv. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Create the .env File​

Save this as .env in the project folder:

.env
GREETING="Hello from the .env file"

The value is in quotes because it contains spaces. uv's parser rejects an unquoted value with spaces: Fixed: "warning: Failed to parse environment file .env at position 6" with uv run shows that warning and the fix.

A .env file usually holds secrets, so add .env to your .gitignore.

Step 2: Write a Script That Reads the Variable​

show_env.py
import os

print(os.environ["GREETING"])

The show_env.py script in a macOS-style code window: it imports os and prints the GREETING environment variable.

Step 3: Run It Without and With the File​

Terminal
uv run show_env.py
uv run --env-file .env show_env.py
Output
Traceback (most recent call last):
File "/Users/you/source/python-basics/show_env.py", line 3, in <module>
print(os.environ["GREETING"])
~~~~~~~~~~^^^^^^^^^^^^
File "<frozen os>", line 729, in __getitem__
KeyError: 'GREETING'
Hello from the .env file

The output in a terminal: uv run show_env.py fails with KeyError: 'GREETING', and uv run --env-file .env show_env.py prints Hello from the .env file.

Without the flag, GREETING didn't exist and Python raised KeyError. With it, uv set the variable for this one run, without changing your terminal's environment.

Common Mistakes​

MistakeWhat happensDo this instead
A value with spaces and no quoteswarning: Failed to parse environment file `.env` at position 6: Hello from the .env file, and the variable is missingQuote the value
Expecting uv to load .env without the flagIt doesn't: KeyError: 'GREETING'Pass --env-file .env, or set UV_ENV_FILE
Committing .env to GitYour secrets end up in the repositoryList .env in .gitignore

Troubleshooting​

SymptomLikely causeFix
The value from the file is ignoredThe same variable is already set in your terminal, and that one winsUnset it in the terminal, or rename one of them

FAQ​

Can I load more than one file?​

Yes. Give --env-file several times; later files override earlier ones.

Do I still need python-dotenv?​

Not to run scripts with uv. Libraries that read .env themselves, such as pydantic-settings, work without this flag.

Conclusion​

uv run --env-file .env show_env.py loaded GREETING from the file and the script printed Hello from the .env file, where the same run without the flag raised KeyError.

Additional Resources​