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

Viewed 311

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.

0 Answers
Related