Skip to main content

Fixed: "warning: Failed to parse environment file .env at position 6" with uv run

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

uv run --env-file .env should have put one variable into my script's environment. Instead uv printed "warning: Failed to parse environment file .env at position 6" and the script failed with KeyError. The cause was one missing pair of quotes. Here's how I found it, with real output from uv 0.13.10.

What I Was Doing​

I was writing an example for loading a .env file with uv: one variable in the file, and a script that prints it.

.env
GREETING=Hello from the .env file
show_env.py
import os

print(os.environ["GREETING"])

What I Expected​

Expected output
Hello from the .env file

What I Got​

Terminal
uv run --env-file .env show_env.py
Output
warning: Failed to parse environment file `.env` at position 6: Hello from the .env file
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'

A terminal where uv run --env-file .env show_env.py prints warning: Failed to parse environment file .env at position 6: Hello from the .env file, then a traceback ending in KeyError: 'GREETING'.

It's only a warning, so uv ran the script anyway, without the variable, and the script failed on its first line.

What I Checked​

1. Where the position points. The message quotes the value, Hello from the .env file. Counting from 0 in that value, position 6 is just after Hello , the start of from: the parser stopped at the first word after a space.

2. uv's documentation on .env files. The configuration files page says uv run reads dotenv files "powered by the dotenvy crate", and its own example puts a value with a space in quotes: MY_VAR='Hello, world!'.

3. The dotenvy parser's example file. dotenvy's own example .env writes spaces in an unquoted value only escaped, as in not\ quoted\ with\ escaped\ space, and puts other values with spaces in quotes, as in "double quoted with # hash in value".

Why It Happens​

uv doesn't read .env itself; the dotenvy parser does. An unquoted value ends at the first unescaped space, so the rest of the line, from the .env file, couldn't be parsed. uv reported it, stopped reading the file there, and ran the script without the variable.

The Fix​

Quote the value:

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

Single quotes work too.

After the Fix​

Terminal
uv run --env-file .env show_env.py
Output
Hello from the .env file

A terminal where uv run --env-file .env show_env.py prints Hello from the .env file.

Common Mistakes​

MistakeWhat happensDo this instead
A value with spaces and no quotesThe parse warning, and the variables are missingQuote the value
Ignoring the warning because the script runsThe script fails later, often with a KeyErrorTreat the warning as an error and fix the file

Troubleshooting​

SymptomLikely causeFix
The warning names another positionA different value has a space, or a quote that doesn't closeCount from the start of the value the warning quotes; Hi there stops at position 3

FAQ​

Do other tools read the same file the same way?​

Not always. Some dotenv readers accept unquoted spaces. Quoting values with spaces works everywhere.

Does one bad line lose the whole file?​

It loses that line and every line after it. In a two-line test with FIRST=one above SECOND=Hi there, the warning named position 3 and only FIRST was set. With the bad line first, neither was. Fix the line in the message, then run again, since a later line can have the same problem.

Conclusion​

The .env value Hello from the .env file had spaces and no quotes, so uv's dotenvy parser stopped at position 6 and the script got no GREETING. Writing it as GREETING="Hello from the .env file" fixed it.

Additional Resources​