334

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 ?

6
  • 27
    Homebew no longer shadows the system installation of python. You can do: export PATH="/usr/local/opt/python/libexec/bin:$PATH" source: discourse.brew.sh/t/… Commented Oct 16, 2017 at 23:16
  • 4
    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. Commented Nov 16, 2017 at 8:40
  • 1
    try... which python2 Commented Feb 10, 2018 at 18:43
  • 3
    It's not recommended anymore to use Homebrew Python for local projects. See Homebrew Python Is Not For You Commented Feb 14, 2022 at 18:17
  • if you're using python3, try 'which python3' . If that works then 'python3 --version' and 'pip3 --version' should work Commented May 2, 2023 at 18:46

27 Answers 27

174

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" 
Sign up to request clarification or add additional context in comments.

10 Comments

This is the correct method on High Sierra. Vote up!
MacOS defaults to the zsh terminal, so chances are it is the .zshrc file and not the .bashrc file that needs to change.
None of the other answers worked for me except this! running macOS Catalina here. Thanks!
For MacOS on Apple Silicon/ARM, this will rather be: export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH" (Homebrew's default location is different)
On my machine (MacOS Ventura, Apple M1) it needed to be: export PATH="/opt/homebrew/opt/python@3/libexec/bin:$PATH" .
|
146

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) 

UPDATE 2023

Brew python changed it's location again and had to update my ~/.zshrc to:

export PATH="$(brew --prefix)/opt/python@3/libexec/bin:$PATH" 

There isn't a .../opt/python/... anymore which is annoying because now I have to specify v3 explicitly, but at least better than having to specify the minor version like in the comments.

7 Comments

it looks like python version should be specified: /opt/homebrew/opt/[email protected]/libexec/bin/
Thank you — that worked for me on my M1 MacBook (and nothing else here did!)
This worked when nothing else did on Mac M1. Thanks!
Note .../opt/python/... is back and there's no need to use python@3 anymore
+1 upvote, I also found this to work in the Update 2023 section of this answer: export PATH="$(brew --prefix)/opt/python@3/libexec/bin:$PATH" Works on my Apple Silicon machine (M2) running Apple Sonoma 14.4.1. After adding that line to my .zshrc, I closed the terminal and re-opened a new one and was able to type python as a command in that terminal and it ran as expected - into a Python shell >>>. Thank you.
|
139

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.

3 Comments

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.
many programs use /usr/bin/python in the header making this not a viable solution.
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
112

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 

7 Comments

why doesn't this happen when I brew install python?
@BenWest because python3, now you get python2 rather then python.
I don't have it enabled.
In my case, I needed to restart the terminal after running this command (as noted by Martijn Courteaux's answer)
this links python@2 for me
|
68

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/

3 Comments

Thank you, all above answers did solve my problem, but your did! Thank you so much!
the problem with this is that it changes it for all users :/, IMO it's better to change something more specific.
@RandallHunt like what and how?
49

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

6 Comments

Confirmed this fixes the issue on High Sierra
You are my hero. Can't believe I didn't realize this solution!
Also need restarting the terminal for apply
$ cd /usr/local/bin $ ln -s python3 python
and I suppose also sym-link pip to pip3 (or pip2)?
|
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.

1 Comment

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.
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.

1 Comment

But it doesn't!
16

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

1 Comment

My editor "PyCharm" was using ~/.bashrc and I was unable to get brew's python working until this little gem. Thanks @Aben
15

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

4 Comments

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?
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.
restart terminal is not necessary, try hash -r
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
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.

Comments

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.

Comments

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.

Comments

5

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.

Comments

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.

Comments

4

On Mac OS X, if you installed Python3 via Homebrew with brew install python3, then you need to run the following command via terminal:

export PATH="/usr/local/opt/[email protected]/libexec/bin:$PATH" 

This is for the latest Python version 3.11. Make sure to edit the command to correspond with your Python version.

Comments

2

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!

2 Comments

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.
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?
2

try this

which python3 

Try typing python3 instead of just python

1 Comment

The issue emerges when some of the scripts are trying to use python command, so you can't simply replace python with python3 everywhere.
2

Add this to your PATH variable in .bashrc or .zshrc

export PATH=$(brew --prefix python)/libexec/bin:$PATH 

This will make sure that when updating your python, it will be updated as well

Comments

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!

Comments

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 

Comments

1

Greeting folks! I have the need to use python 3.10 version to harness its new features. My solved solution at 2022-07-03 is as follows. Have fun coding python!

» rm '/usr/local/bin/pip3.10' » brew link [email protected] » echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc » python3 Python 3.10.5 (main, Jun 23 2022, 17:15:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. 

Comments

1

So I ran into this as well, took me a bit but I figured it out. I had to use which python to find out where python was installed, turns out I had installed it with homebrew, and pip was under the Frameworks dir

 which pip /Library/Frameworks/Python.framework/Versions/3.12/bin/pip 

So then I installed pip with homebrew as well and tried to install it again and got the following

pip install pandas error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try brew install xyz, where xyz is the package you are trying to install. If you wish to install a non-brew-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. If you wish to install a non-brew packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you. Make sure you have pipx installed. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. 

So I then followed what it advised me to do and created a virtual environment in the directory my project was in.

python3 -m venv ./ 

After that I still couldn't get it, pip kept giving me the same error until I ran it like this:

./bin/pip install pandas 

and THAT worked... so now I run

./bin/python3 filename.py 

And I no longer get the error. It's all about finding where your python that you're actually using is, and using that to run your scripts.

I hope this helps someone out there!

1 Comment

You can run source ./bin/activate to enter the venv, then you can just use pip / python3 directly
0

Since High Sierra, you need to use:

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

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

Comments

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' 

Comments

0

Well just in case anyone needed something I needed i.e. just to play around with Python a bit, you may as well call it from terminal with

python3 my_python_file.py 

considering python 3.x is installed, of course, in my case using brew.

Comments

0

Just add the brew's Python directory to the PATH environment variable and don't hard-code the path, rather get it directly from brew, so it doesn't break when it changes:

export PATH="$(brew --prefix python@3)/libexec/bin:$PATH" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.