Where is brew supposed to put its link to its Python binary? - 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?

Related

Python2 version set to Python3 in version check

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.

Homebrew looses track of Python

Pior to my latest update of Homebrew (1.2.4) I had Python located, as indicated by brew --config, with a link to the latest Homebrew version:
Python: /usr/local/bin/python => /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin/python2.7
and both this and the macOS system-provided Python were visible to which -a.
Following the update, I've lost access to this Python (though it still exists, and is available as python2) and just the old system-supplied Python is indicated by listed by brew --config as
Python: /usr/bin/python
and by which -a.
Is this the intended behavior of the latest Homebrew? If not, is there a fix for restoring the access I once had to the latest Homebrew Python?
Note that I also get the following results (which means I can't use brew to fix itself):
$ brew link --overwrite --dry-run python2
Warning: Already linked: /usr/local/Cellar/python/2.7.13_1
To relink: brew unlink python && brew link python
$ brew link --overwrite --dry-run python
Warning: Already linked: /usr/local/Cellar/python/2.7.13_1
To relink: brew unlink python && brew link python
$ brew link --overwrite --dry-run python3
Warning: Already linked: /usr/local/Cellar/python3/3.6.2
To relink: brew unlink python3 && brew link python3
Naively I assume I can fix this "by hand" with
ln -s /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/local/bin/python
But I wonder, given that Homebrew has made such a profound change to the Python configuration, whether more is not going on (that such linking will break). After all, the latest update chose to remove this link when it was present.
This is the intended (though poorly documented) behavior.
To ensure access to the installed Python, it is now necessary to add /usr/local/opt/python/libexec/bin to your PATH either with
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
or by adding it to /private/etc/path.
This will restore access to not only python, but also pip and other Python-related tools, access to which also vanishes with this update. Access to all of these tools is now implemented through symlinks in /usr/local/opt/python/libexec/bin:
2to3# -> ../../bin/2to3-2
easy_install# -> ../../bin/easy_install-2.7
idle# -> ../../bin/idle2
pip# -> ../../bin/pip2
pydoc# -> ../../bin/pydoc2
python# -> ../../bin/python2
python-config# -> ../../bin/python2-config
pythonw# -> ../../bin/pythonw2
smtpd.py# -> ../../bin/smtpd2.py
Note that (without user intervention to change paths as described above) this update appears to violate PEP 394:
The more general python command should be installed whenever any
version of Python 2 is installed and should invoke the same version of
Python as the python2 command

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

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

Can't figure out why brew version of python is not being used

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.

How to make Mac OS use the python installed by Homebrew

I have searched online for a while for this question, and what I have done so far is
installed python32 in homebrew
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
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/python#3.9/libexec/bin
So running
% echo 'export PATH=/opt/homebrew/opt/python#3.9/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)
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
You may try adding this line to your .bash_profile
alias python='python3'
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!!!
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
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.
Install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
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
Use brew link
Confirm by running which python3, the path should be /usr/local/bin/python3

Categories