Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

venv not sticking across subprocess.run on python/windows?

When I make a python venv on Windows:

C:\> mkdir C:\tvenv
C:\> cd C:\tvenv
C:\> python -m venv v

And then create these three files:

t.bat

   call "C:\tvenv\v\Scripts\activate.bat"
   python t1.py

t1.py

  import subprocess
  import sys
  print('T1', sys.executable)
  subprocess.run(['python', 't2.py'])

t2.py

  import sys
  print('T2', sys.executable)

And then I run t.bat:

C:\> t.bat

OBSERVED OUTPUT

T1 C:\tvenv\v\Scripts\python.exe
T2 C:\Program Files\Python38\python.exe

The following happens:

  1. t.bat activates a venv and calls t1.py.
  2. t1.py correctly reports the sys.executable from the venv
  3. t1.py then calls subprocess.run(['python', 't2.py'])
  4. t2.py then reports the system-wide sys.executable, not the one from the venv

ie I would have expected the output to be:

EXPECTED OUTPUT

T1 C:\tvenv\v\Scripts\python.exe
T2 C:\tvenv\v\Scripts\python.exe

as activate.bat sets:

  set PATH=%VIRTUAL_ENV%\Scripts;%PATH%

It puts the venv Scripts dir at the front of the PATH.

so why doesn't subprocess.run(['python']) find the venv python instead of the system-wide one?

Update

I am on latest Windows 10 x64. I just completely reinstalled Python 3.9.1 from the standard python.org Windows installer, and didn't even put it in my PATH. Problem is still present.

like image 977
Andrew Tomazos Avatar asked Jun 28 '26 03:06

Andrew Tomazos


1 Answers

The issue is indeed linked to Windows behavior and in fact the main solution is to use sys.exectuable instead of python to actually launch the expected python binary instead of letting the OS resolving that by himself.
Much more explanations on the issue and how OS are impacting the behavior of subprocess.run command: https://github.com/python/cpython/issues/86207

like image 104
frederickml Avatar answered Jun 29 '26 16:06

frederickml