Reputation: 3836
I have been having issues with openssl and python@2 with brew, which have explained here (unresolved). The documented workaround to reinstall Python and openssl was not working, so I decided I would uninstall and reinstall Python.
The problem is, when you try to install Python 2 with brew, you receive this message:
brew install python@2
Error: No available formula with the name "python@2"
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
git -C "$(brew --repo homebrew/core)" fetch --unshallow
python@2 was deleted from homebrew/core in commit 028f11f9e:
python@2: delete (https://github.com/Homebrew/homebrew-core/issues/49796)
EOL 1 January 2020.
We gave it 1 month more to live so that people had time to migrate.
All in all, developers had 11 years to do their migration.
You can use the `brew extract` command and maintain python@2 in your own
tap if necessary:
https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap
To show the formula before removal run:
git -C "$(brew --repo homebrew/core)" show 028f11f9e^:Formula/[email protected]
If you still use this formula consider creating your own tap:
https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap
Unfortunately I still have a number of brew formulas that depend on Brew's python@2. Those include awscli
, letsencrypt
, pr sshuttle
for example
aws
zsh: /usr/local/bin/aws: bad interpreter: /usr/local/opt/python@2/bin/python2.7: no such file or directory
I don't know how to use this brew extract
command they documented to reinstall Python@2. It needs a formula and a tap. I imagine the formula would be python@2
. I'm not sure what the tap would need to be.
Additionally reinstalling the taps such as aws
or letsencrypt
is not working very well either.
After reinstalling awscli
(brew reinstall awscli
), running aws commands still gives errors.
aws
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:32: SyntaxWarning: "is" with a literal. Did you mean "=="?
if x is 0 or x is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:32: SyntaxWarning: "is" with a literal. Did you mean "=="?
if x is 0 or x is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:34: SyntaxWarning: "is" with a literal. Did you mean "=="?
elif y is 0 or y is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:34: SyntaxWarning: "is" with a literal. Did you mean "=="?
elif y is 0 or y is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:260: SyntaxWarning: "is" with a literal. Did you mean "=="?
if original_result is 0:
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: the following arguments are required: command
Upvotes: 206
Views: 250643
Reputation: 3714
For those showing up here after Apple removed the system python in macOS 12.3, here's how to install and run python2 and python3.
Python 2
python
, python2
-> python 2.7
# Download/run the legacy macOS installer (pick which one for your sys)
https://www.python.org/downloads/release/python-2718/
# Verify it installed correctly
#
# Call it directly
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 --version
# should print Python 2.7.18
# Check the `python` symlink
ls -alf /Library/Frameworks/Python.framework/Versions/2.7/bin/python
# starts with "l" lrwxrwxr-x root admin
# ends with "-> python2"
# Check the `python2` symlink
ls -alf /Library/Frameworks/Python.framework/Versions/2.7/bin/python2
# starts with "l" lrwxrwxr-x root admin
# ends with "-> python2.7"
# Check the binary
ls -alf /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
# starts with "-" -rwxrwxr-x root admin
# is the actual binary
# Check the Apple symlink
ls -alf /usr/local/bin/python
# starts with "l" lrwxrwxr-x root admin
# points to /Library/Frameworks/Python.framework/Versions/2.7/bin/python
# Make sure python is on your PATH. Check or add
# these lines to your ~/.zprofile or shell start script
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Add pip for python2.7
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip2.py
python2 get-pip2.py
# Optionally check for pip updates (in case of post-eol patches)
python2 -m pip install --upgrade pip
# Optionally add some helpers while editing shell profile
alias pip2="python2 -m pip"
alias venv2="virtualenv -p python2"
alias venv3="virtualenv -p python3"
# Optionally some apple-specific std libraries are missing, search
# and download them. Example: plistlib.py
curl https://raw.githubusercontent.com/python/cpython/2.7/Lib/plistlib.py -o /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plistlib.py
# Lastly, there is no symlink /usr/bin/python anymore
# /usr/bin is system protected so you can't add one either
#
# Change your programs to use /usr/local/bin/python
# or google how to disable macOS SIP to make a symlink in /usr/bin
Python 3 (Updated as of macOS 15.1 Sequoia)
python3
-> python 3
brew update
brew install python3
# Turn off the EXTERNALLY-MANAGED flag to:
# - Let you install global packages with `pip3 install`
# - Upgrade pip yourself
#
# WARNING this is old-style python wrangling, be careful
# and know what you're doing before installing global
# packages with this flag turned off
#
# Go to your cellar dir where python3 lives
cd $(brew --cellar python3)
# Find the flag (version-specific directories)
find . -type f -iname 'EXTERNALLY-MANAGED*'
# Go to the dir it's in
cd YOUR_PATH_ABOVE (leave out EXTERNALLY-MANAGED)
sudo mv EXTERNALLY-MANAGED EXTERNALLY-MANAGED.bak
# Add pip for python 3 in case it is missing
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
# Check for pip updates
python3 -m pip install --upgrade pip
# Optionally add a helper in ~/.zprofile
alias venv3="virtualenv -p python3"
Test it out
~ % python --version
Python 2.7.18
~ % python2 --version
Python 2.7.18
~ % python3 --version
Python 3.9.10
# Running older python2
python2 -m pip install...
python2 ...
# Testing the venv2 alias from above
venv2 foo
source foo/bin/activate
pip -V # pip 20... from... python2.7
pip install -y -r req.txt
pip uninstall -y -r req.txt
pip freeze
deactivate
# Testing the venv3 alias from above
venv3 foo3
source foo3/bin/activate
pip -V # pip22... from ...python3.9
pip install -y -r req.txt
pip uninstall -y -r req.txt
pip freeze
deactivate
Troubleshooting via uninstall / reinstall
# Credit to https://www.macupdate.com/app/mac/5880/python/uninstall
# for many of the tips in this section.
# Sometimes there are problems related to accepting xcode
# tool agreement. Open XCode to make sure it finished
# installing its tool updates.
# Remove old python Application installs
# open the apps dir and delete Python 2, 3 via Finder
open /Applications
# Remove old brew installs
brew list | grep python
brew uninstall python
brew uninstall python3
# find/remove lingering unlinked kegs
ls /usr/local/Cellar/ | grep python
# Cleanup binaries
sudo rm -rf /Library/Frameworks/Pyth*
rm /usr/local/bin/pip*
# Cleanup symlinks
which -a python # check results, and rm each one
which -a python2 # check results, and rm each one
which -a python3 # check results, and rm each one
brew cleanup # prunes symlinks
Upvotes: 134
Reputation: 25
You can just download the python installer. https://www.python.org/downloads/release/python-2718/
This works for me.
Upvotes: 0
Reputation: 2401
None of the existing answers really worked for me, so here's my solution in case someone else has a similar use case.
I have python3
installed via homebrew which is symlinked as python
and I wanted python2
to exist alongside it without changing anything.
Compiling from source: At the end of the day, it was easier to compile from source. So this solution assumes you have gcc
installed, which you can do via homebrew install gcc
Sane dependencies: You will also need zlib and other dependencies installed via brew install openssl bzip2 libffi readline sqlite xz zlib
Architecture: I'm on Apple Silicon so I needed the arch
command below, but you should be able to omit it if running on x86. If you're also on Apple Silicon, you'll need Rosetta to be available, otherwise install it with /usr/sbin/softwareupdate --install-rosetta --agree-to-license
Optimizations: I also think you can also remove the --enable-optimizations
flag from ./configure...
if you want faster compilation at the expense of not having optimizations, but I haven't tested it.
The most important bit is running make altinstall
at the end instead of make install
, which ensures your existing python
is left untouched.
Timing consideration: The process will take quite a while to complete even on fast machines. I'm on a M2 Ultra and configure
took a few mins whereas make
took several minutes (long enough to let me write this whole answer!)
About LDFLAGS
and CPPFLAGS
: These environment variables help the configure
script locate necessary libraries for Python features. Setting these ensures your Python build can support secure networking, data compression, and more.
Clean Build Environment: Running make clean
ensures that any previous compilation attempts are cleared out, providing a clean slate for a successful build.
I suggest cd
'ing into some dir like ~/Downloads
before running the below
brew install zlib openssl bzip2 libffi readline sqlite xz
curl -O https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
tar -xf Python-2.7.18.tar.xz
cd Python-2.7.18
Before installing, you need to manually change to a Rosetta shell by typing
arch -x86_64 /bin/zsh
You'll need to do this each time you open a new terminal session and intend to run any part of the installation process under Rosetta on Apple Silicon.
After doing that, you're free to run the install script.
I've pasted this separately from the download script (which you'll only ever need to run once) in case the below fails for some reason:
export LDFLAGS="$LDFLAGS -L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix libffi)/lib -L$(brew --prefix xz)/lib -L$(brew --prefix zlib)/lib"
export CPPFLAGS="$CPPFLAGS -I$(brew --prefix openssl)/include -I$(brew --prefix readline)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix libffi)/include -I$(brew --prefix xz)/include -I$(brew --prefix zlib)/include"
./configure --prefix=/opt/python27 --enable-optimizations
make clean
sudo make altinstall
sudo ln -s /opt/python27/bin/python2.7 /opt/python27/bin/python2
echo 'export PATH="/opt/python27/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
You can now run:
which python2
, which should point to /opt/python27/bin/python2
python2 --version
, which should confirm the installation by showing Python 2.7.18
You'll need to install old (and possibly unsafe) versions of pip and setuptools, sorry
sudo chown -R $(`echo whoami`):staff /opt/python27/
curl -O https://files.pythonhosted.org/packages/b2/40/4e00501c204b457f10fe410da0c97537214b2265247bc9a5bc6edd55b9e4/setuptools-44.1.1.zip
curl -O https://files.pythonhosted.org/packages/53/7f/55721ad0501a9076dbc354cc8c63ffc2d6f1ef360f49ad0fbcce19d68538/pip-20.3.4.tar.gz
tar -xf setuptools-44.1.1.zip
tar -xf pip-20.3.4.tar.gz
cd setuptools-44.1.1
python2 setup.py install
cd ../pip-20.3.4
python2 setup.py install
Upvotes: 0
Reputation: 498
If you just want make python2 work on Mac the most effective way is access https://www.python.org/downloads/ and download the pkg of this version of python.
Seems that homebrew doesn`t support python2 anymore.
best regards.
Upvotes: 3
Reputation: 8237
Not about using Homebrew, but asdf-python
worked for me flawlessly as I was able to install and use Python 2.7.18
from it. (The top-voted answer seems to fail on my system, M1 Max MBP with MacOS 12.3. A few other answers also seem convoluted and didn't work.)
Upvotes: 3
Reputation: 1060
I was getting errors using brew and not able to install.
Error: Installation of python@2 from a GitHub commit URL is unsupported! brew extract python@2 to a stable tap on GitHub instead.
You can direct download python from their site version 2.7.18 and for list of available version click here
Upvotes: 0
Reputation: 19136
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
Upvotes: 400
Reputation: 129
this work for me on m1(12.3.1):
dont use brew, download from python official website directly.
This question has tortured me for a long time :(
Upvotes: 12
Reputation: 11
This should work!!!
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
pip install virtualenv # This will not work, use below
~/Library/Python/2.7/bin/pip install virtualenv
~/Library/Python/2.7/bin/virtualenv --python=/usr/bin/python venv_twisted
source venv_twisted/bin/activate
Upvotes: 1
Reputation: 5922
None of the answers on this page worked for me1 in MacOS Monterey. In case this helps anyone, here is an alternative solution, which is not technically installed directly via Homebrew – just indirectly.
A simple solution for me, that works, is to install Anaconda via Homewbrew, and then create a virtual environment for Python 2.7.
Note: Installing Anaconda will take up some space on your computer, moreso than just simply installing Python 2.7 via Homebrew or Pyenv:
Anaconda is a distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to simplify package management and deployment. The distribution includes data-science packages suitable for Windows, Linux, and macOS. https://en.wikipedia.org/wiki/Anaconda_(Python_distribution)
Very basically, the steps are as follows – but you may want to refer to a full install guide for more details.
Install Anaconda from Homebrew:
brew install --cask anaconda
When installed, create a virtual environment for Python 2.7 in a folder of your choice, in this case for Python 2.7.18:
conda create --prefix=/MY_FOLDER/NAME_OF_ENVIRONMENT python=2.7.18
You can list the environments:
conda env list
Activate the environment via:
conda activate NAME_OF_ENVIRONMENT
Now you can install packages, etc, as usual, using pip install <package>
or alternatively conda install <package>
.
NB: If your Anaconda install is fresh,
You may be prompted to run conda init
once before you can activate the virtual environment.
You may have to run the below in order to be able to find the virtual environment (e.g. via conda list env
) which will add a line in ~/.condarc
:
conda config --append envs_dirs /MY_FOLDER/NAME_OF_ENVIRONMENT
If you are annoyed by the text which may say "Py base" whenever you open Terminal, do the following per this answer, which will add a line in ~/.condarc
:
conda config --set auto_activate_base false
Once you have activated the environment, to shorten the path to it that appears in Terminal whenever it is active, add this row to ~/.condarc
:
env_prompt: ({name})
1 I am not sure if it is because something relating to Python installs is broken on my Mac – so you may not have the same issues. But I was running into problems with all the solutions: everything from installing virtualenv
for the native MacOS Python install did not succeed, build error when attempting to install 2.7.18 via pyenv
, the brew extract
method also failed, etc.
Upvotes: 1
Reputation: 1032
I used methods from this page to install Python 2.7 on Mac for a year. But May 2021 I tried most suggestions on this page and they all failed.
Maybe Python 2.7 is getting harder to install or maybe my new macOS Big Sur 11.4 is causing the problem.
I was able to setup a working Python 2.7 environment in this way by reusing the native Python 2.7.16
Here are the install steps:
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
pip install virtualenv
~/Library/Python/2.7/bin/pip install virtualenv
virtualenv --python=/usr/bin/python venv
source venv/bin/activate
Upvotes: 0
Reputation: 871
For posterity, working on macOS 10.15 (May/2021):
/usr/local/bin/brew tap-new ${USER}/homebrew-python2
/usr/local/bin/brew extract python@2 ${USER}/homebrew-python2
/usr/local/bin/brew install /usr/local/Homebrew/Library/Taps/${USER}/homebrew-python2/Formula/[email protected]
# https://github.com/Homebrew/brew/issues/5734#issuecomment-464705002
/usr/local/bin/brew untap ${USER}/python2
Upvotes: 5
Reputation: 13966
It seems that the homebrew staff really makes it as hard as possible to use Python 2.7 on macOS as they can.
brew extract
link is really not helpful, you need to look for answers here about how to make your own tap from extracted sources.brew extract
command 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/[email protected]
brew install [email protected]
rm [email protected]
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.
Upvotes: 182
Reputation: 866
python@2
from a local tapThe following method works with the current version (c9b8a3ef6
) of brew
:
$ brew tap-new <user>/homebrew-python2
$ brew extract python@2 <user>/homebrew-python2
$ brew install /usr/local/Homebrew/Library/Taps/<user>/homebrew-python2/Formula/[email protected]
The brew tap-new
command creates a new local tap template in /usr/local/Homebrew/Library/Taps/<user>/homebrew-python2
. The tap name needs a <user>
and a <repo>
component separated by a /
. The actual values are arbitrary. The naming above follows the conventions from How to Create and Maintain a Tap. If you wanted to push the tap to GitHub you would use your GitHub username as user. Pushing to GitHub is not necessary (and was not performed in the instructions above).
The brew extract
commands extracts the recent version of formula from the repos history into the given (local) tap. In our case [email protected]
is extracted.
The brew install
command finally installs the formula.
The method discussed above (installing an old version of the formula from a GitHub commit URL) does not work anymore for python@2
with the current version of brew
(c9b8a3ef6
), it produces the following error:
$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/[email protected]
Updating Homebrew...
==> Auto-updated Homebrew!
Updated Homebrew from 88f17b8b6 to c9b8a3ef6.
...
Error: Calling Installation of python@2 from a GitHub commit URL is disabled! Use 'brew extract python@2' to stable tap on GitHub instead.
Upvotes: 60
Reputation: 713
Please check following command (I am using it on macOS 10.13, it is possible that for a newer macOS it will work without source compilation):
brew install pr0d1r2/python2/[email protected] --build-from-source
Upvotes: 18