Python packages in the wrong place after installing Homebrew Python?

After installing Homebrew Python on a system with Apple Python installed, the latest entries listed by sys.path using Homebrew Python,

 /Library/Python/2.7/site-packages /usr/local/lib/python2.7/site-package 

This is the opposite of the expected order. Shouldn't you search for search packages first? (In fact, should this not be the only search?) What is implied in the documentation . Where is it installed and how can I (or should I change it)?

Or is that how Brewed Python should work? It is designed to prevent duplication of packages and assumes that any package in the site-packages should remain there if it is not explicitly deleted, and then subsequently installed (in Brew); with the exception of pip and setuptools , which are duplicated (and placed first in the Brewed Python path).

+8
python homebrew
source share
1 answer

This is the intended behavior. The rationale for this is that you can continue to use your old installed modules, even though you are now using the new homeprewed Python.

Now this has some disadvantages, for example, some libraries, such as numpy, will not work in different versions of Python, so if you installed numpy, it would be imported from the old site-packages and would not work.

There are at least two ways to modify sys.path :

Use .pth file:

Python will select this from some of the built-in locations (for example: ~ / Library / Python / 2.7 / lib / python / site-packages / homebrew.pth). This adds to sys.path , which is not perfect, but has the advantage of not being selected by Python 3. This is currently the recommended method . You can achieve this with:

 echo "$(brew --prefix)/lib/python2.7/site-packages" > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth 

Install PYTHONPATH :

This is added to sys.path , it has a flaw that is global for all versions of python, so it is not recommended if you intend to use different versions of python. You can do this by adding to your .bash_profile :

 export PYTHONPATH=`brew --prefix`/lib/python2.7/site-packages:$PYTHONPATH 

I personally used option 2 with homebrew-python (now I use and recommend Anaconda). My reasons were that I was not interested in system Python or Python 3 then.

+9
source share

All Articles