Sherwood Wang
Sherwood Wang

Reputation: 715

What's the meaning of __PYVENV_LAUNCHER__ environment variable?

I accidentally noticed that when a child process is launched with subprocess.Popen, two environment variables named __PYVENV_LAUNCHER__ and _ are set to paths of the python interpreter and the script respectively. These two variables seem to affect the behavior of pip. I wonder the exact meaning of them.

Upvotes: 7

Views: 1269

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123830

__PYENV_LAUNCHER__ is an implementation detail of the way a Python framework build on Mac works.

In a framework build (a special build that allows you to run GUI apps powered by Python), Apple places strict limits on what you can do with the process. To break out of those limitations, the Python binary is actually a wrapper that then launches the 'real' Python binary as a child process (the Resources/Python.app/Contents/MacOS/Python binary in the same framework package).

To let the child process know what the path was that was used to launch the wrapper binary, the wrapper sets the __PYVENV_LAUNCHER__ environment variable, and the actual Python binary then uses that instead of argv[0] (set by the OS). This is important in case of hardlinked copies of the launcher binary, such as those used in a virtualenv. Hence the name PYVENV in the variable.

The variable really shouldn't leak out beyond the point it has done its job, so it is being removed from the environment once read in future Python releases.

Upvotes: 3

Related Questions