On 2016-11-06, Michael Torrie <torr...@gmail.com> wrote: > I'm guessing that it notices by examining the path it was launched from > and looks for virtual environment files relative to that path.
Indeed, the mysterious thing is what are "virtual environment files"? The official docs say "A venv is a directory tree which contains Python executable files and other files which indicate that it is a venv." but don't explain what they mean by "other files". > Here's what a random site said about this (I didn't search long enough > to find the official documentation on python.org): > > "When Python is starting up, it looks at the path of its binary (which, > in a virtual environment, is actually just a copy of, or symlink to, > your system’s Python binary). It then sets the location of sys.prefix > and sys.exec_prefix based on this location, omitting the “bin” portion > of the path." Aha! This is progress. OK, I've done some more investigation and it looks like the above description is not exactly accurate. The magic is actually all in site.py by the looks of it. This looks at argv[0] (effectively - it's actually using sys.executable) and then looks for ./pyvenv.cfg and ../pyvenv.cfg relative to that file. Only if one of those files exists does it then set sys.prefix to .. relative to sys.executable and then sys.path is set using various hard-coded paths relative to sys.prefix. So the answer to my original question is that Python knows it's in a venv "if there exists a file called pyvenv.cfg in the same directory as or the parent directory of the python exeuctable". It also appears that this feature was added in Python 3.3, so being able to run the venv python directly presumably only works on that version and later. -- https://mail.python.org/mailman/listinfo/python-list