You can use pyenv to install python with:
brew install pyenv
pyenv install 2.7.18
Optionally set it to your global default:
pyenv global 2.7.18
Nice article on why using pyenv is better than using brew to manage your python installation.
To make python binary available globally, add shims to PATH:
PATH=$(pyenv root)/shims:$PATH
Answer from donturner on Stack OverflowYou can use pyenv to install python with:
brew install pyenv
pyenv install 2.7.18
Optionally set it to your global default:
pyenv global 2.7.18
Nice article on why using pyenv is better than using brew to manage your python installation.
To make python binary available globally, add shims to PATH:
PATH=$(pyenv root)/shims:$PATH
It seems that the homebrew staff really makes it as hard as possible to use Python 2.7 on macOS as they can.
- The linked
brew extractlink is really not helpful, you need to look for answers here about how to make your own tap from extracted sources. - The linked commit: 028f11f9e is wrong, as it contains the already deleted file.
- The
brew extractcommand doesn't even work correctly, because of the @ in the package name.
The solution is very simple though, you just need to download the latest known commit and install from that file:
cd ~
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb
brew install python@2.rb
rm python@2.rb
There might be a warning about this being "unstable", which I don't understand as a commit in a Git history is as stable as you can get.
You can get any Python release, including the last Python 2, from the official download site:
https://www.python.org/downloads/release/python-2718/ → macOS 64-bit installer
If, like me, you need it for development you should use PyEnv:
Here's a great tutorial: https://dev.to/jordicuevas/how-to-install-python2-in-a-macbook-m1-with-brew-bhi
TL;DR:
brew install pyenv
pyenv install 2.7.18
export PATH="$(pyenv root)/shims:${PATH}"
echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
pyenv init
(Follow instructions provided. I had to add to ~/.zprofile and ~/.zshrc.)
Make sure you load the changes by running source ~/.zprofile and source ~/.zshrc.
pyenv shell 2.7.18
Now, the python command should target Python 2 in the shell/Terminal.
I have done this sort of thing in two ways - however both do not coexist with homebrew although I suspect there should be a way similar to the macports one.
General package manager
The first is using Macports as the package manager. There are separate ports for several python versions 2.x and 3.x currently 2.4, 2.5, 2.6, 2.7 and 3.1, 3.2, 3.3 3.4 and 3.5. For example to install 3.4 do sudo port install python34 There is also a python_select port that running it allows you to choose which version of python is run by /opt/bin/python. e,g, to select macports python 2.7 port select --set python python27
Each package manager needs to run as the only package manager otherwise it will be confused with unexpected different versions of libraries and executables on its paths.
From comments the similar homebrew way is given in https://stackoverflow.com/a/4158763/151019 Short answer seems to be brew switch the equivalent to port_select
Python version managers
The other way is install direct from python.org This will install a python into /usr/local/bin I think this installs a python and python3 executable. The issue with Homebrew is that it assumes that things in /usr/local are installed by it and so brew doctor will report these python.org pythons and libraries as issues and as this question shows also the frameworks in /Library will be problematic.
EDIT 8/2018 *** I would now suggest using conda either miniconda or anaconda. This is another package manager but based and mainly meant for python, it also gives the equivalent of python's virtual environments and pyenv (as per other answer) and so each project can be in a different python and different libraries (Macports only switches python version for all projects)
For Python in macOS, use pyenv:
brew install pyenv
To install a specific Python version, run:
pyenv install <version>
To select a version for every command run with the current folder:
pyenv local 3.5.0
Note that you must update PATH in your environment to find pyenv's python3 before any other:
PATH="~/.pyenv/shims:${PATH}"
With recent version of OSX(13.X) the path is moved to /etc/paths.d/ Add a file eg: pyenvpath to /etc/paths.d/pyenvpath Add the path to that file.
To list available versions:
pyenv versions
See https://gist.github.com/Bouke/11261620 for more information.