How to make Mac OS use the python installed by Homebrew

Question:

I have searched online for a while for this question, and what I have done so far is

  1. installed python32 in homebrew

  2. changed my .bash_profile and added the following line to it:

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

but when I close the terminal and start again, I type ‘which python’, it still prints:

/usr/bin/python

and type ‘python –version’ still got:

Python 2.7.2

I also tried the following instruction:

brew link –overwrite python

or try to remove python installed by homebrew by running this instruction:

brew remove python

but both of the above two instructions lead to this error:

Error: No such keg: /usr/local/Cellar/python

can anybody help, thanks

Asked By: photosynthesis

||

Answers:

If you want to install Python 3 using Homebrew:

$ brew install python3
==> Downloading http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
Already downloaded: /Library/Caches/Homebrew/python3-3.3.0.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/python3/3.3.0 --enable-ipv6 --datarootdir=/usr/local/Cell
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0/share/python3
==> Downloading https://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz
Already downloaded: /Library/Caches/Homebrew/distribute-0.6.35.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Downloading https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/pip-1.3.1.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Caveats
Homebrew's Python3 framework
  /usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework

Distribute and Pip have been installed. To update them
  pip3 install --upgrade distribute
  pip3 install --upgrade pip

To symlink "Idle 3" and the "Python Launcher 3" to ~/Applications
  `brew linkapps`

You can install Python packages with
  `pip3 install <your_favorite_package>`

They will install into the site-package directory
 /usr/local/lib/python3.3/site-packages
Executable python scripts will be put in:
 /usr/local/share/python3
so you may want to put "/usr/local/share/python3" in your PATH, too.

See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python

Once installed update your system PATH variable, add the next line to ~/.bash_profile

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

And then:

$ source ~/.bash_profile

Now launch Python:

$ python3
Python 3.3.0 (default, Mar 26 2013, 10:01:40) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can check python3 path:

$ which python3
/usr/local/bin/python3
Answered By: jabaldonedo

You may try adding this line to your .bash_profile

alias python='python3'
Answered By: Pan Long

From $ brew info python:

This formula installs a python2 executable to /usr/local/bin.
If you wish to have this formula’s python executable in your PATH then add
the following to ~/.bash_profile:
export PATH=”/usr/local/opt/python/libexec/bin:$PATH”

Then confirm your python executable corresponds to the correct installation:

$ which python or

$ python --version

Answered By: kip2

brew install python or brew info python output mentions:

Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been installed into
/opt/homebrew/opt/[email protected]/libexec/bin

So running

% echo 'export PATH=/opt/homebrew/opt/[email protected]/libexec/bin:$PATH' >> ~/.zprofile
% source ~/.zprofile
# For bash use ~/.bash_profile.

gets you those symlinks created by Homebrew – python=python3, pip=pip3 etc

% python --version
Python 3.9.10
% pip --version
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Answered By: Callum M

I came through the same issue and did some research. I found that someone has created a bug for the same issue under the azure/cli repository. You can find that issue here. I am providing the same solution here which was very easy and fixed my issue:

Most probably the Brew is broken and needs some patching or fixing. So run brew doctor command which will give you a summary about what is happening.
Below is what I got:

mymac:bin sidmishra$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: The following directories do not exist:
/usr/local/sbin

You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/LibSideSyncOSX9.dylib
  /usr/local/lib/ss_conn_lib.dylib

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  python

The awesome stuff about the command brew doctor is that it not only tells you issues but also suggests you the solution steps in most of the cases.
So, I ran all the commands suggested by the brew and to link I ran the following command:

brew link python

Above command threw me an error:

mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks/Python.framework

It seems that /urs/local/Frameworks doesn’t have enough rights for my current user. So, I ran the following command and gave enough rights to my current user:

sudo chown -R $(whoami) /usr/local/Frameworks/

After running above command I ran linking command again, and it worked!!!

mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... 1 symlinks created

Now run following command to get the current selected python version:

python --version

Above command should give you 3.7.1 (as of 21st Dec 2018) or new version for the python. There might be a chance that your Mac would have python2 set by default. If the version is not python3 then you have to a couple of steps to use the latest python3 over python2 version. Here are the steps:

Using Shell:

  • Open ~/.bash_loginor ~/.bash_profileor ~/.cshrcor ~/.profileor ~/.tcshrcor ~/.zprofile, whatever shell you are using for commands, in edit mode. You may have to use sudo to edit them.
  • Add following steps to it:

    PATH=”/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}”
    export PATH

  • Also, add following for backup:

    alias python=python3

OR
Using homebrew:

Run following commands to unlink python2 and link python3:

mymac$ brew unlink python@2
mymac$ brew link python@3

Above will unlink python2 and link python3.

Hope some of you will get helped from this answer.

Good Day!!!

Answered By: S.Mishra

Installing with Homebrew is recommended on macOS. That being said, Python 2.7 comes with Mac OS; however, it is deprecated and will be removed soon. As such, you should be using Python3 and newer.

  1. Install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  1. Run brew install python

Once Python is installed, Homebrew will say that the installation is complete, but that you already have Python 2.7 installed. This is nice, but we want to set it to actually see python3 as an option

  1. Use brew link
  2. Confirm by running which python3, the path should be /usr/local/bin/python3
Answered By: Justin Rice
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.