Python 3.8.5 Not Launching

Eryk Sun eryksun at gmail.com
Sat Oct 3 06:40:39 EDT 2020


On 10/3/20, Gertjan Klein <gklein at xs4all.nl> wrote:
>
> I tried to find out what happens, using your other code:
>
>  >>> import win32con, win32api
>  >>> access = win32con.PROCESS_QUERY_LIMITED_INFORMATION
>  >>> hproc = win32api.OpenProcess(access, False, pid)
>  >>> executable = win32process.GetModuleFileNameEx(hproc, None)
>  >>> print(executable)
> C:\Temp\Python\Console\venv\Scripts\python.exe
>  >>> hproc = win32api.OpenProcess(access, False, os.getpid())
>  >>> win32process.GetModuleFileNameEx(hproc, None)
> 'C:\\dev\\Python\\Python38\\python.exe'
>
> So, if I understand this correctly, the console is owned by the venv
> Python, but the running process is the installed Python executable. I'm
> lost! How did that latter one get involved?

In Windows, venv defaults to copying binaries instead of creating
symlinks. Starting with 3.7.2, venv copies a python.exe launcher (a
custom build of the py.exe launcher) instead of copying the base
executable and DLLs [1]. The launcher finds and spawns the base
python.exe, and waits for it to exit. If you run the launcher from
Explorer, it's the launcher that allocates and owns the console
session. The base python.exe inherits the console session.

The primary benefit of copying a launcher is that existing virtual
environments don't have to be updated or recreated when the base
installation is updated. The primary downside is that the parent
process has a handle for and PID of the launcher process instead of
the base Python process. This causes problems if the parent tries to
manually duplicate a handle to the child, or vice versa, since it's
actually duplicating the handle to the launcher process.

This is particularly a problem for the multiprocessing module. It has
to detect whether it's in a launcher-based virtual environment by
comparing sys.executable with sys._base_executable. If they're
different files, it executes sys._base_executable and sets the child's
"__PYVENV_LAUNCHER__" environment variable to sys.executable (the venv
launcher) in order to make it use the virtual environment.

[1] https://docs.python.org/3.7/whatsnew/3.7.html#notable-changes-in-python-3-7-2


More information about the Python-list mailing list