187

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/python@2.rb

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
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Pauline
  • 3,566
  • 8
  • 23
  • 39
  • Thanks for all the answers here, but sadly none of them are reliable nowadays. Save yourself the hassle and just download it from https://www.python.org/downloads/release/python-2718/ – sudo Oct 23 '22 at 23:08

13 Answers13

341

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
Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
donturner
  • 17,867
  • 8
  • 59
  • 81
  • 13
    This worked the best for me after macOS 12.3 Monterey removed python 2.7. – Leon Shaner Apr 13 '22 at 17:34
  • 10
    Sorry for a newbie question but do I get a global `python` or `python2` to work? After following the steps above, `which python` says "python not found". (`which python3` works and it returns `/opt/homebrew/bin/python3`). – Borek Bernard Apr 21 '22 at 15:17
  • This is a good solution but leaves you without a working /usr/bin/python, which is sometimes required (e.g. whenever it is used under a shebang line - instead of in the /usr/bin/env form) – d4vidi Jul 20 '22 at 15:43
  • 1
    Not working in macOS 12.6 Intel) - I had such hope. For the record, binutils is not installed – goofology Oct 16 '22 at 20:38
  • 3
    I don't recommend this because it tries to build Python from source, which will take forever and possibly fail (as it did for me on my M1 Mac). Plus `pyenv` is one more moving part that you probably don't need. – sudo Oct 23 '22 at 23:04
  • I did this and 'python -m pip install --upgrade pip'. Worked on my macOS Ventura! – diva Feb 14 '23 at 23:05
  • No support for [t]csh - deal breaker for me. – Conrad Mar 13 '23 at 20:00
  • brew install pyenv pyenv install 2.7.18 Worked for me and allowed me to link 'python' to the verison of python i wanted to run by default, thanks. – Ian LeBlanc Jul 13 '23 at 17:03
179

It seems that the homebrew staff really makes it as hard as possible to use Python 2.7 on macOS as they can.

  1. The linked brew extract link is really not helpful, you need to look for answers here about how to make your own tap from extracted sources.
  2. The linked commit: 028f11f9e is wrong, as it contains the already deleted file.
  3. The 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/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.

hyperknot
  • 13,454
  • 24
  • 98
  • 153
  • 14
    Just putting the link to the PR here, where python@2 was removed (merged Feb. 4th 2020) https://github.com/Homebrew/homebrew-core/pull/49796 – petschki Mar 12 '20 at 20:14
  • 8
    @petschki not the answer we asked for, but the answer we wanted to know! – ThinkBonobo Apr 16 '20 at 19:47
  • Thanks, running `brew upgrade python@2` should work too. – ICeZer0 Jun 11 '20 at 00:23
  • 1
    This also helped to fix the following error: `ERROR:root:code for hash md5 was not found. Traceback (most recent call last): File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in globals()[__func_name] = __get_hash(__func_name) File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ...` – Elliot Aug 12 '20 at 21:10
  • 22
    This (`brew install `) does not seem to work anymore with the current version of Homebrew, it produces the following error: `Error: Calling Installation of python@2 from a GitHub commit URL is disabled! Use 'brew extract python@2' to stable tap on GitHub instead.` – jvf Sep 03 '20 at 13:53
  • 7
    This is how I did it: `cd ~` - `wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb` - `brew install python@2.rb` – MrTomRod Sep 19 '20 at 14:54
  • I just tried the command in this solution and get the error `Error: Calling Installation of python@2 from a GitHub commit URL is disabled! Use 'brew extract python@2' to stable tap on GitHub instead.` – Nathan Loyer Oct 30 '20 at 15:02
  • 1
    @MrTomRod Your solution worked for me. The real answer is always in the comments. – Nathan Loyer Oct 30 '20 at 15:10
  • 4
    I updated the answer to the wget based version by @MrTomRod. The Homebrew team really goes an extra lengh in trying to ban Python 2.7 on macOS: deleting all forum topics mentioning it, deleting GitHub issues, banning GitHub accounts opening an issue and now also blocklisting the commit id in source code. – hyperknot Oct 30 '20 at 18:31
  • I had to do 2 additional things: `brew uninstall python@2` before installing the rdb. If I didn't do that, I was getting errors that it was already installed. Then `brew reinstall openssl@1.1` After that, my old python code worked again. Thanks to all above. – Wayne Jan 31 '21 at 19:06
  • 3
    seems like this no longer works on the M1 Mac ... some type of build error ... – Dewey Feb 02 '21 at 21:03
  • 10
    Not working from the last commit... `==> Downloading https://ghcr.io/v2/homebrew/core/python/2/manifests/2.7.17_1 ##O#- # curl: (22) The requested URL returned error: 404 Error: Failed to download resource "python@2_bottle_manifest" Download failed: https://ghcr.io/v2/homebrew/core/python/2/manifests/2.7.17_1` – Sagar Ranglani Apr 24 '21 at 12:13
  • 4
    This doesn't work on M1 / Apple Silicon mac as this formula doesn't support it. It gives the following error: "configure: error: Unexpected output of 'arch' on OSX" This is likely because brew dropped support for python2 before the M1 was released. – nobled Jul 01 '21 at 18:25
  • 1
    @hyperknot Given what you listed, the Homebrew team goes way beyond what is acceptable in their attempt to save the world from Python 2. There are reasons to still support Python packages on Python 2, let alone the fact that macOS 11.6 (Big Sur) still includes Python 2. – Andreas Maier Dec 12 '21 at 15:18
  • 3
    Anyone here trying to install Python 2 on the latest release where the system one has been removed entirely, MacOS 12.3 Monterey? This method no longer works. At first `brew` complains about an unrecognized bottle name but even after I comment out the `bottle do` part of the formula, the installation fails with a Python error: `File "./setup.py", line 805, in detect_modules (tuple(int(n) for n in dep_target.split('.')[0:2]) AttributeError: 'int' object has no attribute 'split'` – phette23 Mar 15 '22 at 17:02
  • 6
    OK I downloaded from the Python releases page and the installer worked, though the script to install certificates does not. https://www.python.org/downloads/release/python-2716/ – phette23 Mar 15 '22 at 17:29
  • 13
    Got below error while execute brew install python@2.rb Error: python@2: Invalid bottle tag symbol – loongman May 31 '22 at 10:32
  • 2
    Any solution for this error? python@2: Invalid bottle tag symbol – Dinesh Dec 06 '22 at 22:44
  • 1
    Error: python@2: Invalid bottle tag symbol – Yaro Apr 27 '23 at 09:37
116

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/

# 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 the helpers like easy_install back onto your path
# In your ~/.zprofile or whatever bash/shell profile equivalent
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# 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

python3 -> python 3

brew update
brew install python3

# 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
Josh Hibschman
  • 3,148
  • 1
  • 25
  • 27
60

How to install python@2 from a local tap

The 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/python@2.7.17.rb

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 python@2.7.17.rb is extracted.

The brew install command finally installs the formula.

Why is this necessary?

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/python@2.rb
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.
jvf
  • 866
  • 7
  • 13
  • This was helpful as I was trying to install an old version of openssl. One note: `` in the example above is your Github username. – heyrolled Sep 03 '20 at 16:44
  • Thank you for the updated answer. does not have to be your github username; it can be anything or nothing. Using your login name on your local machine is a good way to not have it collide with other users on your computer, if any. – sirdodger Sep 04 '20 at 20:23
  • 4
    This should be the answer since the accepted answer is no longer working. – KTU Sep 09 '20 at 01:27
  • 2
    These instructions don't work for me unfortunately. When attempting `brew extract python@2 /homebrew-python2`, I get: *Error: python@2: undefined method `[]' for nil:NilClass* – P A N Oct 29 '21 at 11:58
16

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/python@2.7.17 --build-from-source
Marcin Nowicki
  • 655
  • 6
  • 4
  • 7
    This worked but this also reinstalled my Python3. Which means it removed all the packages I had installed with it. Thanks though. – Danyal Sep 14 '20 at 15:51
10

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 :(

yanchao
  • 109
  • 1
  • 3
  • 1
    @Dos This nonetheless provides an answer to the _underlying problem_; please see [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – awwright May 23 '22 at 23:15
  • 2
    This solved my problem. Really simple, thaks – saga56 Jun 28 '22 at 11:01
5

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/python@2.7.17.rb

# https://github.com/Homebrew/brew/issues/5734#issuecomment-464705002
/usr/local/bin/brew untap ${USER}/python2
ab77
  • 841
  • 12
  • 14
3

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.)

xji
  • 7,341
  • 4
  • 40
  • 61
2

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.

1

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,

  1. You may be prompted to run conda init once before you can activate the virtual environment.

  2. 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

  3. 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

  4. 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.

P A N
  • 5,642
  • 15
  • 52
  • 103
1

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
  • 2
    There are **seven existing answers** to this question, including a top-voted, accepted answer with over **one hundred fifty votes**. Are you _certain_ your solution hasn't already been given? If not, why do you believe your approach improves upon the existing proposals, which have been validated by the community? Offering an explanation is _always_ useful on Stack Overflow, but it's _especially_ important where the question has been resolved to the satisfaction of both the OP and the community. Help readers out by explaining what your answer does different and when it might be preferred. – Jeremy Caney Mar 10 '22 at 00:28
  • 1
    @JeremyCaney Highly voted answers may become obsolete over time and prompt the need for new, entirely different answers. The "there's already a highly upvoted answer" argument is generally a poor one. – awwright May 23 '22 at 23:22
  • @awwright: Reread my critique. It's not that the highly voted answer must be correct. It's that this answer was submitted to a question without any explanation. If the top-voted answer is out-of-date, and this addresses that, then this answer should state that clearly. – Jeremy Caney May 24 '22 at 00:48
0

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

  • Install pip manually
  • Setup a virtual environment
  • Install dependencies with pip in virtual environment

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
Sami Badawi
  • 977
  • 1
  • 10
  • 22
  • Where does `virtualenv` get installed using this method? I get `command not found: virtualenv` although it appears to have been installed somewhere. – P A N Oct 30 '21 at 08:18
0

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

R7G
  • 1,000
  • 1
  • 10
  • 15
  • 1
    Even though this is not what this questions is looking for, to my understanding, I am not sure why this answer was -1, since currently this is a solution to the problem to install python2 in mac OSs. – Hunter8 Nov 27 '22 at 18:51