Homebrew/homebrew — pull request #28597 by dakcarto (2014-04-22 02:38:04+00:00)
Title: Fix up sys.path for brewed python

With the fix implemented for PR #26377 in 6f22450, the following happens, i.e. duplicate user-installed modules from system Python take precedence:

In my setup I have `docutils` installed in `/Library/Python/2.7/site-packages` (via system Python) and in `$(brew --prefix)/lib/python2.7/site-packages` (via Homebrew Python).

If I set `PYTHONPATH=$(brew --prefix)/lib/python2.7/site-packages` (or even without that env var) I would expect `docutils` to be imported from Homebrew's `site-packages` _always before_ anything from `/Library/...`'s.

Here are the results (where `python` executable is verified to be Homebrew's):

```python
python -c "import docutils ; print docutils"

```
Whereas, nixing the `site` module import (adding **-S** option), or making the `sitecustomize.py` conditional False again, returns expected result:

```python
python -S -c "import docutils ; print docutils"

```

This fix rearranges sys.path so all Homebrew paths are first, but maintains order of any PYTHONPATHs or .pth manipulations.

**However...**

* _Is this the right approach?_ I think it makes sense that when using the brewed Python modules in its `site-packages` should take preference over duplicates in `/Library/...`.

* _Why are the brewed Python paths stripped to begin with?_

The comment mentions:

```
# Remove /System site-packages, and the Cellar site-packages
# which we moved to lib/pythonX.Y/site-packages. Further, remove
# HOMEBREW_PREFIX/lib/python because we later addsitedir(...).
```
I understand why `/System` paths are stripped, but the `site.addsitedir('#{site_packages}')` call should not add duplicate paths. So, I'm at a loss as to why stripping those paths is necessary.

Lastly, there is a comment mentioning PYTHONPATH being processed _after_ `sitecustomize.py`, but I found in my tests that any defined paths in PYTHONPATH are already in `sys.path` when referenced inside the `sitecustomize.py` script.