243

I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with

brew install python 

The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default

which python

give me

/usr/bin/python

and /usr/bin/python is not a symlink

How can I do to make python brew flavour to be my default python ?

nbro
  • 13,796
  • 25
  • 99
  • 185
sam
  • 3,098
  • 2
  • 20
  • 19
  • 23
    Homebew no longer shadows the system installation of python. You can do: `export PATH="/usr/local/opt/python/libexec/bin:$PATH"` source: https://discourse.brew.sh/t/what-happened-to-the-standard-python-package/935 – George Oct 16 '17 at 23:16
  • 3
    I tried all the mentioned answers, /usr/local/bin comes before /usr/bin in my $PATH. I tried brew link and unlink, brew doctor didn't report the /usr/bin being before /usr/local/bin an issue. Still when I do which python, I get /usr/bin/python and not the one installed with brew. – Ujwal Ratra Nov 16 '17 at 08:40
  • try... which python2 – Fraser Feb 10 '18 at 18:43
  • 1
    It's not recommended anymore to use Homebrew Python for local projects. See [Homebrew Python Is Not For You](https://justinmayer.com/posts/homebrew-python-is-not-for-you/) – fn control option Feb 14 '22 at 18:17

22 Answers22

129

As you are using Homebrew the following command gives a better picture:

brew doctor

Output:

==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. This is an issue if you eg. brew installed Python.

Consider editing your .bash_profile to put: /usr/local/bin ahead of /usr/bin in your $PATH.

bbaassssiiee
  • 5,454
  • 1
  • 39
  • 50
Vishal
  • 18,919
  • 19
  • 75
  • 92
  • 4
    Make sure that you don't have /user/local/bin TWICE in your path. For some reason in my PATH it appeared both before and after /usr/bin and it was driving me crazy. – vinaut Oct 02 '13 at 19:01
  • 1
    many programs use /usr/bin/python in the header making this not a viable solution. – eatingthenight Jul 07 '15 at 20:15
  • 6
    for brew in High Sierra only `python2` is in /usr/local/bin, for just `python`; set `export PATH="/usr/local/opt/python/libexec/bin:$PATH"` in `~/.bash_profile` – tormodatt Jan 31 '18 at 13:45
114

As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Pamput
  • 2,266
  • 2
  • 15
  • 16
97

See: How to symlink python in Homebrew?

$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python
Community
  • 1
  • 1
akauppi
  • 15,618
  • 12
  • 83
  • 106
66

Quick fix:

  1. Open /etc/paths
  2. Change the order of the lines (highest priority on top)

In my case /etc/paths looks like:

/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin

If you want to know more about paths in OSX I found this article quite useful:

http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/

Bijan
  • 24,204
  • 7
  • 74
  • 69
48

I did "brew install python" for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system's python.

When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:

/usr/local/bin $: ln -s python2 python

Kirkland
  • 3,237
  • 1
  • 15
  • 17
Sachin Khot
  • 1,292
  • 2
  • 12
  • 17
29

python formula now uses python3(v3.6.5 for now), brew will link the directory:

/usr/local/opt/python -> ../Cellar/python/3.6.5

it will also link the binary:

/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3

If you still need to use python2.x, use:

brew install python@2

To use homebrew's python, just put its directory in PATH, for bash:

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

for fish:

set -x PATH /usr/local/opt/python/libexec/bin $PATH

Note:

  1. doing this will shadow the system default version of python
  2. homebrew used to link python to /usr/local/share/python in older versions.
PickBoy
  • 1,194
  • 1
  • 12
  • 20
  • This was the trick for me. By default, brew installs `python3` in your path - _not_ an exec called `python`.. Specifically referencing `/usr/local/opt/python/libeexec/bin` ahead of other directories in my path sorted things out. – cam8001 Jul 09 '18 at 02:37
24

Homebrew does NOT replace stuff in "/usr/bin". You'll just want to put "/usr/local/bin" ahead of "/usr/bin" in your path, then "which python" will give you "/usr/local/bin/python".

Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.

Adam Vandenberg
  • 18,893
  • 7
  • 52
  • 55
16

For Apple Silicon machines, the path are slightly different. After running brew install python, you must ensure your ~/.zshrc uses the correct Homebrew paths:

# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"

# Homebrew: Python
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"

Results:

% which python
/opt/homebrew/opt/python/libexec/bin/python

% python --version
Python 3.9.9

% which pip
/opt/homebrew/opt/python/libexec/bin/pip

% pip -V
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
TruMan1
  • 30,176
  • 50
  • 162
  • 301
15

Modify your $PATH, Add this in your bashrc or bash_profile:

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

more click here: Issue #89791

Aben
  • 151
  • 1
  • 4
  • My editor "PyCharm" was using ~/.bashrc and I was unable to get brew's python working until this little gem. Thanks @Aben – JamesDeHart Dec 11 '16 at 05:27
14

I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.

Martijn Courteaux
  • 65,802
  • 45
  • 192
  • 282
  • 1
    This is all I needed to do. I'm not sure when to do this, though. It sounds like just execute `brew install python` and then, restart terminal, and nothing else? No need to `brew unlink` & `brew link`? – Con Antonakos Dec 04 '16 at 23:40
  • Right after installed python using brew, "which python" give me the correct local python path, but "python" command indicated that it was the system installed one invoking. Restart terminal fixed it. – Michelle Feb 20 '17 at 06:50
  • 2
    restart terminal is not necessary, try `hash -r` – georgexsh Sep 08 '17 at 05:13
  • JFC, I was losing my mind until I read this. I already did `brew unlink & brew link` several times. Only needed to restart the terminal – Dominic Jan 08 '18 at 08:46
10

You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.

Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You're just asking for trouble if you do.

JoshAdel
  • 62,453
  • 24
  • 136
  • 135
7

python now points to python3, if you need python 2 then do: brew install python@2 and then in your .zshrc or .bashrc file export PATH="/usr/local/opt/python@2/libexec/bin:$PATH" Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4. That's the behavior I'm used to seeing in my terminal.

pixel 67
  • 1,340
  • 16
  • 20
6

I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv --python option). Using tools like python_select is almost always a bad idea.

Mikhail Korobov
  • 21,149
  • 8
  • 72
  • 64
5

Use pyenv instead to install and switch between versions of Python. I've been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.

Consult pyenv's github page for installation instructions. Basically it goes like this: - Install pyenv using homebrew. brew install pyenv - Add a function to the end of your shell startup script so pyenv can do it's magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

  • Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
  • Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
  • If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project's folder that specifies the python version. Pyenv will look override the global python version with the version in that file.
mpoisot
  • 7,141
  • 4
  • 26
  • 21
3

Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:

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

After that, it should work correctly.

Dmytro Melnychuk
  • 1,918
  • 19
  • 21
1

Just do:

brew install python
brew link python

After doing that, add this to your bashrc or bash_profile:

alias python='/usr/local/bin/python2'

Enjoy!

  • It sounds like you ran into the same problem I did. All the links that brew creates, even with the `link` commands in answers here, in `/usr/local/bin` are for `python2` instead of `python`. Using an alias to work around this seems like a bad idea, but I haven't found anything better yet. – Patrick M Dec 05 '17 at 17:15
  • Rather than alias, I just copied it `cp /usr/local/bin/python2 /usr/local/bin/python` and it seemed to work well. Is this an even uglier work-around? – kingb12 Dec 20 '17 at 18:59
1

You can edit /etc/paths. Here is mine:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Then add a symlink for the python version. In my case

$ cd /usr/local/bin
$ ln -s python3 python

Voila!

1

If you are fish shell

echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish
KelaKing
  • 126
  • 8
0

Since High Sierra, you need to use:

sudo chown -R $(whoami) $(brew --prefix)/*

This is because /usr/local can no longer be chowned

zaolian
  • 1,127
  • 3
  • 11
  • 21
0
brew link python

And you must create/add an alias for python and put it in your .zprofile (Located in Users/username folder, if you press Shift+command+.

This must point to your homebrew python installation location.

alias python ='opt/homebrew/bin/python3'
-1

try this

which python3

Try typing python3 instead of just python

-9

No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.

Andreas Jung
  • 1
  • 19
  • 73
  • 119
  • 5
    there is no risk in os x to having homebrew's python come before the system's python in the path. This is very common. – Steven Noble Nov 07 '11 at 04:59
  • 4
    Also, the need to use a non-Apple Python is real. I've been trying to get matplotlib, NumPy and SciPy up and running with the 'default' (aka Apple) Python. It's something about its treating of 32/64 bits that simply blows extensions apart. Very, very, very glad to have a nice Brew instead. And by default ('brew install python') Brew does exactly what you mean - it installs the second Python to a separate structure, not overriding paths (which is precisely why this SO question arises). – akauppi Feb 01 '13 at 11:37