Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

brew-installed Python not overriding system python

I just used brew to install Python 3 on OS X. The python3 command now starts the interpreter using brew Python 3.6, but python still opens the interpreter with the default system Python 2.7.

My understanding was that, by default, brew Python should now override system Python. (I.e., see Order of /usr/bin and /usr/local/bin and more in $PATH). In my PATH, /usr/local/bin comes before /usr/bin, so it shouldn't be a PATH issue. I have tried restarting Terminal, with no effect.

Here is my full PATH in case that is relevant.

/Users/**/.rvm/gems/ruby-1.9.3-p362/bin:/Users/**/.rvm/gems/ruby-1.9.3-p362@global/bin:/Users/**/.rvm/rubies/ruby-1.9.3-p362/bin:/Users/**/.rvm/bin:/Users/**/.rvm/bin:/Users/**/Python/PmagPy/programs/conversion_scripts2/:/Users/**/Python/PmagPy/programs/conversion_scripts/:/Users/**/Python/PmagPy/programs:/usr/local/heroku/bin:./bin:/usr/local/sbin:/usr/local/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin

Why isn't brew Python taking precedence? And how can I fix (or troubleshoot) this? If I can't find another option, I can create an alias, but I prefer to understand what's happening and get to the root of the problem.

Update:

I checked out the "possible duplicate" question, but my issue doesn't appear to be a linking problem:

 ~ brew link --overwrite --dry-run python
Warning: Already linked: /usr/local/Cellar/python/3.6.4_4
To relink: brew unlink python && brew link python
 ~ 
like image 675
J Jones Avatar asked Mar 21 '18 17:03

J Jones


People also ask

Where does Homebrew install Python on Mac?

For brewed Python, modules installed with pip or python3 setup.py install will be installed to the $(brew --prefix)/lib/pythonX. Y/site-packages directory (explained above).

How do you check Python is installed or not?

Python is probably already installed on your system. To check if it's installed, go to Applications>Utilities and click on Terminal. (You can also press command-spacebar, type terminal, and then press Enter.) If you have Python 3.4 or later, it's fine to start out by using the installed version.


1 Answers

TL;DR Add the following to your .bash_profile (or equivalent):

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Explanation

It seems python via homebrew is now handled differently (see https://docs.brew.sh/Homebrew-and-Python).

  • python3 points to Homebrew’s Python 3.x (if installed)
  • python2 points to Homebrew’s Python 2.7.x (if installed)
  • python points to Homebrew’s Python 2.7.x (if installed) otherwise the macOS system Python. Check out brew info python if you wish to add Homebrew’s 3.x python to your PATH.

Checking out brew info python hints at what you need to do:

Unversioned symlinks python, python-config, pip etc. pointing to python3, python3-config, pip3 etc., respectively, have been installed into /usr/local/opt/python/libexec/bin

The hint being that you therefore have to add /usr/local/opt/python/libexec/bin before /usr/bin in your path (not /usr/local/bin as stated in some sources e.g. https://docs.python-guide.org/starting/install3/osx/)

See also https://github.com/Homebrew/homebrew-core/issues/15746

like image 99
Claire Furney Avatar answered Sep 22 '22 12:09

Claire Furney