Skip to content

fix: resolve dotenv run commands against the .env PATH on Windows - #730

Open
summeriver13 wants to merge 2 commits into
theskumar:mainfrom
summeriver13:fix/venv-python-on-windows
Open

summeriver13 wants to merge 2 commits into
theskumar:mainfrom
summeriver13:fix/venv-python-on-windows

Conversation

@summeriver13

@summeriver13 summeriver13 commented Oct 11, 2026 •

Copy link
Copy Markdown

What this fixes

On Windows, dotenv run ignores the PATH from .env: CreateProcess resolves the
executable against the calling process's environment, so the one passed to Popen is not
used (os.execvpe on POSIX does use it). With a venv on that PATH,
dotenv run -- python -c "import sys; print(sys.executable)" prints the launching
interpreter.

The fix

executable=shutil.which(command[0], path=cmd_env.get("PATH")) — path= is required,
otherwise shutil.which falls back to os.environ["PATH"]. Not found → None, so the
existing Command not found: ... path is unchanged; executable= (not replacing
command[0]) keeps the child's argv[0].

Verification

test_run_resolves_command_using_dotenv_path puts a stub command in a directory reachable
only through the .env PATH: it fails without the fix on Windows (Command not found: dotenv-test-cmd) and passes with it elsewhere. pytest tests/: 237 passed, 52 skipped, no
regressions; tox -e lint: ruff passes, mypy reports only the pre-existing
test_fifo_dotenv.py:15 os.mkfifo error.

#508 uses the same call without path= (it is also CONFLICTING), so the .env PATH stays
ignored.

AI disclosure: an AI agent (pi) prepared the root cause, patch and test; I reproduced the bug
and ran the checks above.

Fixes #373

Windows' CreateProcess looks up the executable using the calling process'
environment, ignoring the one passed to it. So `dotenv run` did not find
commands only reachable through the PATH set in .env, and a bare `python`
resolved to the interpreter that started the process rather than the
virtualenv's.

Resolve the command with shutil.which() against the environment we are
about to pass instead.

Fixes theskumar#373

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] dotenv cli not used windows venv if it activated

1 participant