I would like to install some packages into a third-party site-packages directory (beyond the standard system locations). Is there any way to set this up such th
There was PEP 370 specifically addressing the creation of per-user site-packages directories, to deal with the situation where the user has no admin access to the system-wide site-packages.
For example on Unix (including Mac OS), and assuming one is using Python 3.6, one can create the following directory and place .pth files inside there
~/.local/lib/python3.6/site-packages
Take a look at the site module. It provides the function addsitedir which should do what you want.
The easiest way to use this would be to create a file named sitecustomize.py or usercustomize.py and place it in a current PYTHONPATH directory (or any directory that ends up on sys.path) with the following contents:
import site
site.addsitedir('/usr/local/lib/python2.7')
When Python is starting an attempt is made to import sitecustomize and then usercustomize, which is why this works. From the site documentation:
After these path manipulations, an attempt is made to import a module named
sitecustomize, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with anImportErrorexception, it is silently ignored.After this, an attempt is made to import a module named
usercustomize, which can perform arbitrary user-specific customizations, ifENABLE_USER_SITEis true. This file is intended to be created in the user site-packages directory (see below), which is part ofsys.pathunless disabled by-s. AnImportErrorwill be silently ignored.