Skip to content

Instantly share code, notes, and snippets.

@dstufft
Last active August 29, 2015 13:58
Show Gist options
  • Save dstufft/9972581 to your computer and use it in GitHub Desktop.
Save dstufft/9972581 to your computer and use it in GitHub Desktop.
Python uses the os.py module as a sort of sentinel value to determine where the
stdlib is. It will look in a relative directory first. So if you have a file
structure like…
.
├── bin
│ └── python
└── lib
└── python2.6
└── os.py
Then the os.py in that directory will trigger the Python interpreter to believe
that the stdlib should be in that lib directory. virtualenv exploits this and
symlinks os.py into that directory, and then also installs a custom site.py [1]
in that directory which does all the heavy lifting of actually setting up our
sys.path in the virtual environment.There is also a custom
distutils/__init__.py file as well that is installed to do some monkey patching
there. In order for the site.py functionality to work, virtualenv symlinks a
number of things into the lib directory so that they are available for import
prior to site.py having been executed to finish setting up the sys.path so that
the regular stdlib is visible in the virtual environment.
[1] https://github.com/pypa/virtualenv/blob/develop/virtualenv_embedded/site.py
[2] https://github.com/pypa/virtualenv/blob/develop/virtualenv_embedded/distutils-init.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment