22

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 that .pth files therein are respected?


Background: I'm using OS X, virtualenv, and homebrew. There are a few packages (notably wxPython in my case) that do not install nicely through pip into virtualenv. In these cases, there are homebrew packages that work in their stead. Homebrew creates a third site-packages folder in /usr/local/lib/python2.7. I'd like to simply point to this folder and leave the maintenance of all items there under brew's control. It seems, however, that this is not possible.

I'm certainly not the only one interested in this issue. I'd wager a good number of the generic 'pth files not working' questions and posts online that I've stumbled across are related to this issue. Is there a good solution?

Community
  • 1
  • 1
mbauman
  • 30,958
  • 4
  • 88
  • 123

2 Answers2

23

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 an ImportError exception, it is silently ignored.

After this, an attempt is made to import a module named usercustomize, which can perform arbitrary user-specific customizations, if ENABLE_USER_SITE is true. This file is intended to be created in the user site-packages directory (see below), which is part of sys.path unless disabled by -s. An ImportError will be silently ignored.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • Do sitecustomize.py or usercustomize.py automatically get run on startup? Edit: [yes, yes they do](http://docs.python.org/library/site.html). Thanks! – mbauman May 21 '12 at 23:17
  • @MattB. Yes, edited my answer with some additional info. The documentation comes from the top of the site module page that I linked in my answer. – Andrew Clark May 21 '12 at 23:18
  • 1
    I still don't understand fully how work site, but: (1) shouldn't it by addsitedir('/usr/local'), i.e. the alternative prefix (but it does not work for me), or (2) addsitedir('/usr/local/lib/python2.7/site-packages') the path to append, and in that second case, why not just adding it to the PYTHONPATH ? – Juh_ Jun 06 '12 at 13:07
  • Answer to myself: the difference of using site.addsitedir is that the .pth file are "executed". However a colleague of mine is telling me that this feature is not necessary anymore. Would anyone disagree ? – Juh_ Jun 06 '12 at 14:19
  • Yes - if you are using egg files or a particular path structure. – Danny Staple Mar 26 '13 at 16:12
  • 2
    Hint: use `usercustomize`. On some linux distros a global sitecustomize exists, on some not. This can lead to confusing behaviour. No linux distro provides a usercustomize. – guettli Nov 11 '16 at 08:35
  • @guettli: usercustomize seems disabled in venvs. Any idea what to do then? – ingomueller.net Aug 27 '19 at 08:30
1

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

divenex
  • 15,176
  • 9
  • 55
  • 55