How to link home brew python version and set it as default

PythonMacosHomebrew

Python Problem Overview


I just switched from MacPorts to HomeBrew. After installing all the required XCode versions and other software I tried installing python using homebrew: I think it successfully installed, but when I do which python it still shows me 2.7.3 which I think is the version shipped with Mountain Lion.

which python
/usr/local/bin/python

python --version
Python 2.7.3

so I tried to install again

brew install python --framework --universal
Warning: python-2.7.5 already installed, it's just not linked

But it says python 2.7.5 already install and not linked, I tried to do brew link python

That led me to following message so, I have no idea what I should be doing:

Linking /usr/local/Cellar/python/2.7.5... Warning: Could not link python. Unlinking...

Error: Could not symlink file: /usr/local/Cellar/python/2.7.5/bin/smtpd2.py
Target /usr/local/bin/smtpd2.py already exists. You may need to delete it.
To force the link and overwrite all other conflicting files, do:
  brew link --overwrite formula_name

To list all files that would be deleted:
  brew link --overwrite --dry-run formula_name

Python Solutions


Solution 1 - Python

After installing python3 with brew install python3 I was getting the error:

Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

After typing brew link python3 the error was:

Linking /usr/local/Cellar/python/3.6.4_3... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

To solve the problem:

sudo mkdir -p /usr/local/Frameworks
sudo chown -R $(whoami) /usr/local/*
brew link python3

After this, I could open python3 by typing python3

(From https://github.com/Homebrew/homebrew-core/issues/20985)

Solution 2 - Python

In the Terminal, type:

brew link python

Solution 3 - Python

If you used

brew install python

before 'unlink' you got

brew info python
/usr/local/Cellar/python/2.7.11

python -V
Python 2.7.10

so do

brew unlink python && brew link python

and open a new terminal shell

python -V
Python 2.7.11

Solution 4 - Python

I think you have to be precise with which version you want to link with the command brew link python like:

brew link python 3

It will give you an error like that:

> Linking /usr/local/Cellar/python3/3.5.2... Error: Could not symlink bin/2to3-3.5 Target /usr/local/bin/2to3-3.5 already exists.

You may want to remove it:

rm '/usr/local/bin/2to3-3.5'

To force the link and overwrite all conflicting files:

brew link --overwrite python3

To list all files that would be deleted:

brew link --overwrite --dry-run python3

but you have to copy/paste the command to force the link which is:

brew link --overwrite python3

I think that you must have the version (the newer) installed.

Solution 5 - Python

On OS X High Sierra, I had to do this:

sudo install -d -o $(whoami) -g admin /usr/local/Frameworks
brew uninstall --ignore-dependencies python
brew install python
python --version # should work, returns 2.7, which is a Python thing (it's weird, but ok)

credit to https://gist.github.com/irazasyed/7732946#gistcomment-2235469

I think it's better than recursively chowning the /usr/local dir, but that may solve other problems ;)

Solution 6 - Python

This answer is for upgrading Python 2.7.10 to Python 2.7.11 on Mac OS X El Capitan . On Terminal type:

brew unlink python

After that type on Terminal

brew install python

Solution 7 - Python

brew switch to python3 by default, so if you want to still set python2 as default bin python, running:

brew unlink python && brew link python2 --force

Solution 8 - Python

You can follow these steps.

$ python3 --version  
$ brew unlink python@2
$ brew link python3   
$ python3 --version   

All steps

Solution 9 - Python

For those looking for a version re-link using brew, I've found this command useful:

brew unlink python@3.9 && brew link python@3.10

It is possible that some errors appear, like:

Error: Could not symlink bin/pip3
Target /usr/local/bin/pip3
already exists. You may want to remove it:
  rm '/usr/local/bin/pip3'

To force the link and overwrite all conflicting files:
  brew link --overwrite python@3.10

To list all files that would be deleted:
  brew link --overwrite --dry-run python@3.10

So following brew advice and running:

rm '/usr/local/bin/pip3'
brew link --overwrite [email protected]

Just worked for me. To check if symlinks are ok, run: ls -l /usr/local/bin/python*, you should see something like:

/usr/local/bin/python3 -> ../Cellar/[email protected]/3.10.2/bin/python3
/usr/local/bin/python3-config -> ../Cellar/[email protected]/3.10.2/bin/python3-config
/usr/local/bin/python3.10 -> ../Cellar/[email protected]/3.10.2/bin/python3.10
/usr/local/bin/python3.10-config -> ../Cellar/[email protected]/3.10.2/bin/python3.10-config

Solution 10 - Python

The problem with me is that I have so many different versions of python, so it opens up a different python3.7 even after I did brew link. I did the following additional steps to make it default after linking

First, open up the document setting up the path of python

 nano ~/.bash_profile

Then something like this shows up:

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

The thing here is that my Python for brew framework is not in the Library Folder!! So I changed the framework for python 3.7, which looks like follows in my system

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/usr/local/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH

Change and save the file. Restart the computer, and typing in python3.7, I get the python I installed for brew.

Not sure if my case is applicable to everyone, but worth a try. Not sure if the framework path is the same for everyone, please made sure before trying out.

Solution 11 - Python

I wouldn't recommend playing around with the symlinks, it's not just to where python points, its the entire env around it as well that maybe mixing version

I would do this:

$ brew install pyenv
$ pyenv install 3.7.3
$ pyenv global 3.7.3

Solution 12 - Python

I use these commands to solve it.

mkdir /usr/local/lib
mkdir /usr/local/lib/pkgconfig
brew link python

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionadd-semi-colonsView Question on Stackoverflow
Solution 1 - PythonlenoohView Answer on Stackoverflow
Solution 2 - PythonHerculesView Answer on Stackoverflow
Solution 3 - PythonRolf SchulzeView Answer on Stackoverflow
Solution 4 - PythonslapashView Answer on Stackoverflow
Solution 5 - PythonChaim EliyahView Answer on Stackoverflow
Solution 6 - PythonVikram DuttView Answer on Stackoverflow
Solution 7 - PythonEric GuoView Answer on Stackoverflow
Solution 8 - PythonAnkit Kumar RajpootView Answer on Stackoverflow
Solution 9 - PythoncamikillerView Answer on Stackoverflow
Solution 10 - PythonYolandaView Answer on Stackoverflow
Solution 11 - PythonRicky LeviView Answer on Stackoverflow
Solution 12 - Pythonuser7842596View Answer on Stackoverflow