So I was trying to install OpenCV in on the MacOS following this tutorial: https://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/
In step 3 the versions of python should be python 2.x checked with python --version and python 3.y checked with python3 --version. However on both commands I am getting Python 3.6.4. How can I fix this? I have tried to install python 2 again with brew install python#2 and the output is python#2 2.7.14_1 is already installed. which python shows /usr/local/bin/python whereas which python3 shows /usr/local/bin/python3.
If you installed Python 2 and Python 3 with Homebrew, then the Python 2 binary is named python2, but will not be linked into /usr/local unless you use brew link with the --force flag.
See brew info python#2:
$ brew info python#2 | grep Caveats -A 4
==> Caveats
This formula installs a python2 executable to /usr/local/opt/python#2/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#2/libexec/bin:$PATH"
$ brew info python#2 | grep 'not symlinked' -A 2
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
You can run
/usr/local/opt/python\#2/bin/python2
if you need to run it directly, or you can force homebrew to link it in anyway with:
brew link --force python#2
at which point /usr/local/bin/python2 will be added.
The tutorial is rather outdated; how homebrew handles Python has changed. Just use python2 wherever it uses python.
To further address the tutorial:
brew linkapps is deprecated
the homebrew/science tap is deprecated, the formulas in that tap have been migrated, opencv3 can be installed without additional taps.
the current version of the opencv3 natively supports both Python 2 and 3, no configuration switches required
the formula directly depends on the Python 2 and 3 formulae, installing opencv3 will automatically install Python.
So just run brew install opencv3, follow any additional instructions that command prints (could be none).
Do make sure you update Homebrew as there were some dependencies missing (see my bug report with Homebrew). If you don't, you'll have to run brew install hdf5 tesseract to install dependencies that should have been optional.
The tutorial is almost entirely obsolete now.
I used to have python and python3 installed. So I could decide which python to use. I now updated python with homebrew and it migrated python3 to python. How do I run python2 now?
When I check the installation I get:
$ python -V
Python 3.6.4
$ python2 -V
-bash: python2: command not found
$ python3 -V
Python 3.6.4
If I try to simply reinstall python2 I get:
$ brew install python2
Warning: python#2 2.7.14_1 is already installed
However I cannot use python2.
python2: command not found
You're missing the symbolic link that Homebrew makes from the Cellar to the actual bin directory on your path.
Use:
brew link python2
to fix that.
You may run into a warning:
Warning: python#2 is keg-only and must be linked with --force Note that doing so can interfere with building software.
See the accepted answer to this SO question for some details on that.
In most case, you can then safely use
brew link --force python2
if you're not planning building your own libraries that require the source code (libpython.so and Python.h) for Python 2.
If you do require the source code, you'll need to provide the include paths and library paths to e.g. /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/include/python2.7 and /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/. But that's a different topic or question.
To use pip for Homebrew's Python 2, best to use is
python2 -m pip <command>
Then you can clearly see what Python your pip command goes with, and keep it apart from Python 3 (which would be python3 -m pip).
Note
If you have Homebrew problems, first cause of action is to run
brew doctor
The error messages are usually quite helpful to fix at some of the problems.
It seems that Homebrew has changed things again - as I had both python 2.7.x and 3.x.x installed through Homebrew for the same reasons. After the initial update, python2 would no longer work, but if you used python#2 you would get access as before.
Now however, they have seemed to partially revert some of these changes. Calling python now points to the keg-only 2.7.x homebrew installation instead of the 3.x.x installation. In addition, python#2 no longer works, but python2 no does. python3 still points to the 3.x.x installation as before.
[NOTE: I have not modified my ~/.bash_profile for any of these changes to occur.]
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
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)
In the Terminal, type:
brew link 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
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 python#3.10
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/python#3.10/3.10.2/bin/python3
/usr/local/bin/python3-config -> ../Cellar/python#3.10/3.10.2/bin/python3-config
/usr/local/bin/python3.10 -> ../Cellar/python#3.10/3.10.2/bin/python3.10
/usr/local/bin/python3.10-config -> ../Cellar/python#3.10/3.10.2/bin/python3.10-config
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.
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 ;)
You can follow these steps.
$ python3 --version
$ brew unlink python#2
$ brew link python3
$ python3 --version
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
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
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
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.
I use these commands to solve it.
mkdir /usr/local/lib
mkdir /usr/local/lib/pkgconfig
brew link python
I recently tried to upgrade my Python version via brew and now I am stuck with OSX's system Python because Brew does not seem to be creating a link to it's Python binary.
brew link python outputs (yes I've tried relinking):
Warning: Already linked: /usr/local/Cellar/python/2.7.5
To relink: brew unlink python && brew link python
brew --prefix outputs:
/usr/local
And yet ls /usr/local/python outputs:
ls: /usr/local/python: No such file or directory
Am I misunderstanding how brew is supposed to be working? Shouldn't it be putting a link to a python binary in my /usr/local/bin directory?
In a new shell:
brew install python outputs:
Warning: python-2.7.5 already installed
brew link python outputs:
Warning: Already linked: /usr/local/Cellar/python/2.7.5
To relink: brew unlink python && brew link python
echo $PATH outputs:
/Users/captbaritone/.rvm/gems/ruby-2.0.0-p0/bin:/Users/captbaritone/.rvm/gems/ruby-2.0.0-p0#global/bin:/Users/captbaritone/.rvm/rubies/ruby-2.0.0-p0/bin:/Users/captbaritone/.rvm/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/local/git/bin:/usr/local/MacGPG2/bin:/usr/texbin:/opt/local/bin
And yet which python still outputs:
/usr/local/bin/python
And python --version still outputs:
Python 2.7.1
Here is a lead:
ls -l /usr/local/bin/python outputs:
ls: /usr/local/bin/python: No such file or directory
Any ideas of what I might be doing wrong, or what else I could try?
You didn't link python properly. If you type brew install python on your terminal, it may output as Warning: python-2.7.5 already installed, it's just not linked.
If then, you need to link your python install by typing brew link python. If you face any conflict because of some earlier site-packages, just type as brew link --overwrite python.
Now it should work fine.