How to Load a .env File with uv run --env-file
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
uv run --env-file <file> <command>
| Part | What 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:
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
import os
print(os.environ["GREETING"])

Step 3: Run It Without and With the File
uv run show_env.py
uv run --env-file .env show_env.py
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

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
| Mistake | What happens | Do this instead |
|---|---|---|
| A value with spaces and no quotes | warning: Failed to parse environment file `.env` at position 6: Hello from the .env file, and the variable is missing | Quote the value |
Expecting uv to load .env without the flag | It doesn't: KeyError: 'GREETING' | Pass --env-file .env, or set UV_ENV_FILE |
Committing .env to Git | Your secrets end up in the repository | List .env in .gitignore |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The value from the file is ignored | The same variable is already set in your terminal, and that one wins | Unset 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
- Environment variable files —
--env-file,UV_ENV_FILEand--no-env-file.
