python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

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

Open Niccolum opened this issue 2 years ago • 6 comments

I use vscode on windows

Command:

python -m venv venv
.venv\Scripts\activate
pip install python-dotenv[cli]
dotenv run -- python -c "import sys; print(sys.executable)"

Expected: \path\to\venv\Scripts\python.exe

Real: \path\to\system\python.exe

If needed more info - I will try to provide it ASAP.

Cc: @scad89 help to find this issue

System: Windows 10.

python -V Python 3.8.5

Niccolum avatar Jan 27 '22 21:01 Niccolum

Found interesting issue in CPython about it https://bugs.python.org/issue38905

Have you any ideas how to solve this?

I have idea with one crutch:

command = [
    sys.executable if s == "python" and os.name == "nt" else s 
    for s in command
]

Somewhere before https://github.com/theskumar/python-dotenv/blob/master/src/dotenv/cli.py#L153

But I didn't test this yet

Also we can add check sys.version_info in this crutch, but i haven't all supported python version on windows. I see you have tox, so we can come up with test for that and add different OS in tox (platform parameter) for check it for that, if you support this change.

Niccolum avatar Jan 27 '22 22:01 Niccolum

UPD: crutch is working!

Niccolum avatar Jan 29 '22 00:01 Niccolum

Any chance the fix above can be merged? (assuming it works)

Indiana3714 avatar Sep 01 '22 05:09 Indiana3714

Any fix for this? Still running into this issue in windows

EliiseS avatar Mar 14 '24 10:03 EliiseS

Same issue here.

MahrRah avatar Mar 14 '24 10:03 MahrRah

You can do something like that (assuming cmd.exe)

dotenv run %VIRTUAL_ENV%\Scripts\python -c "import sys; print(sys.executable)"

Current behavior is just Python behavior. Compare

python -c "import sys; print(sys.executable)"

and

python -c "import subprocess; subprocess.run(['python','-c', 'import sys; print(sys.executable)'])"

Also, see the warning in Popen documentation https://docs.python.org/3/library/subprocess.html#subprocess.Popen

Following the suggestions in the warning I created a pull request #508 . I believe resolving against the PATH is what most of the users expect. You can try it with:

pip install python-dotenv@git+https://github.com/Bajron/python-dotenv@dotenv-run-resolve-path-for-upstream[cli]

Most likely you need to uninstall the old version first as the version string is still the same and pip might not apply the changes because of that.

Bajron avatar Mar 15 '24 18:03 Bajron