Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Post a Comment for "Python Packages In Wrong Location After Installing Homebrew Python?"