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 would use pyenv You can install it:
$ brew install pyenv
To enable pyenv in your Bash shell, you need to run:
$ eval "$(pyenv init -)"
To do this automatically for Bash upon startup, add that line to your ~/.bash_profile. 1
Usage:
Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:
$ pyenv install 2.7.5
You can check the versions you have installed with:
$ pyenv versions
And you can switch between python versions with the command:
$ pyenv global 3.3.1
Also you can set a python version for the current directory with:
$ pyenv local 3.5.2
You can check by running python --version:
$ python --version
Python 3.5.2
1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.
You can have both versions installed at the same time.
For Homebrew >=1.5.0:
Since 1st March 2018 the python formula will be upgraded to Python 3.x, while a new python@2 formula will be added for Python 2.7, specifically.
See changes announcement here or the final doc about using Homebrew for Python here.
For older Homebrew:
For Python 2.x:
brew install python
For Python 3.x:
brew install python3
Now, you will have both the versions installed in your machine. When you want to use version 2, use the python executable. When you want to use version 3, use the python3 executable.