20

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
 ~ 
J Jones
  • 3,060
  • 4
  • 26
  • 43
  • Possible duplicate of [How to link home brew python version and set it as default](https://stackoverflow.com/questions/19340871/how-to-link-home-brew-python-version-and-set-it-as-default) – Brian Kung Mar 21 '18 at 17:57

3 Answers3

39

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

Claire Furney
  • 2,115
  • 3
  • 24
  • 36
  • 2
    After spending an hour messing with it, this finally did it! Thank you!!! – mariordev Jun 05 '19 at 17:07
  • I know you're not supposed to overwrite the system installation of python at `/usr/bin/python` on macOS, so why is this different? I know that it's not actually overwriting, but I would think that this would have the same effect of making programs that rely on `python` use `python3` when they are trying to use python 2. – Rylan Polster May 06 '20 at 01:28
  • 1
    You're right, but unless the script is compatible with both python 2.x and 3.x, I believe it is good practice to explicitly state the version if it will be run by the system in some manner e.g. with `#!/usr/bin/env python3`. See https://stackoverflow.com/a/19305076/1265167 – Claire Furney May 06 '20 at 19:16
  • 1
    I'm not worried about my own scripts, but rather system scripts that require python2. Overnight I realized, though, that these scripts are probably unaffected as they likely state explicitly that they need python 2 by using something like `#!/usr/bin/env python2`, which I think is what you're saying. Thanks for your help! – Rylan Polster May 06 '20 at 19:33
5

One-liner to get homebrew python working:

zsh

echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc

bash

echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc

Explanation:
>> filename appends at the end of the file
source filename reloads the file

Florent Roques
  • 2,424
  • 2
  • 20
  • 23
-1

I tried a few of the proposed solutions in How to link home brew python version and set it as default, but none of them worked. Ultimately I solved this by symlinking python3 --> python:

ln -s /usr/local/bin/python3 /usr/local/bin/python
J Jones
  • 3,060
  • 4
  • 26
  • 43