Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python packages in wrong location after installing Homebrew Python?

After installing Homebrew's Python onto a system with an established Apple Python, the last entries listed by sys.path using Homebrew's Python are

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

This is the reverse of the order I expect. Shouldn't Homebrew's packages be searched first? (In fact, shouldn't it be the only place searched?) That's what's implied in the documentation. Where is it set and how can I (or should I) change it?

Or is this the way Brewed Python is supposed to work? Is it designed to prevent duplicate packages and assume that any package in the system site-packages is meant to stay there unless explicitly uninstalled and then subsequently installed (into Brew's); with the exception of pip and setuptools which are duplicated (and put first in Brewed Python's path).

like image 657
orome Avatar asked Jan 14 '15 21:01

orome


1 Answers

That is the intended behaviour. The rationale behind it is that you can keep using your old installed modules despite the fact that you are now using a new homebrewed Python.

Now this has some drawbacks, for example some libraries like numpy, won't work across different Python versions, so if you had installed numpy it will be imported from the old system's site-packages and won't work.

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

Use a .pth file:

Python will pick that from some of the builtin locations (ex: ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth). This appends to sys.path which is not ideal but has the advantage that it won't be picked by Python 3. It 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

Set PYTHONPATH:

This gets prepended to sys.path, it has the drawback that is global to all python versions so it is not recommended if you are going to use different python versions. You can do it by adding to your .bash_profile:

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

I personally used option 2 with homebrew-python (I now use and recommend Anaconda). My reasons were that I didn't care about system's Python or Python 3 at the time.

like image 185
elyase Avatar answered Oct 24 '22 07:10

elyase