How to reinstall python@2 from Homebrew?

58,453

Solution 1

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/[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.

Solution 2

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

Solution 3

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/[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.

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/[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.

Solution 4

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

Solution 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/[email protected]

# https://github.com/Homebrew/brew/issues/5734#issuecomment-464705002
/usr/local/bin/brew untap ${USER}/python2
Share:
58,453
Pauline
Author by

Pauline

I like cats, I just think they're nice

Updated on October 31, 2021

Comments

  • Pauline
    Pauline over 2 years

    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
    
  • petschki
    petschki about 4 years
    Just putting the link to the PR here, where python@2 was removed (merged Feb. 4th 2020) github.com/Homebrew/homebrew-core/pull/49796
  • ThinkBonobo
    ThinkBonobo about 4 years
    @petschki not the answer we asked for, but the answer we wanted to know!
  • ICeZer0
    ICeZer0 almost 4 years
    Thanks, running brew upgrade python@2 should work too.
  • Elliot
    Elliot almost 4 years
    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.framewo‌​rk/Versions/2.7/lib/‌​python2.7/hashlib.py‌​", line 147, in <module> globals()[__func_name] = __get_hash(__func_name) File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framewo‌​rk/Versions/2.7/lib/‌​python2.7/hashlib.py‌​", line 97, in __get_builtin_constructor raise ValueError('unsupported hash type ' + name) ...
  • jvf
    jvf over 3 years
    This (brew install <raw url>) 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.
  • heyrolled
    heyrolled over 3 years
    This was helpful as I was trying to install an old version of openssl. One note: <user> in the example above is your Github username.
  • sirdodger
    sirdodger over 3 years
    Thank you for the updated answer. <user> 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.
  • KTU
    KTU over 3 years
    This should be the answer since the accepted answer is no longer working.
  • Danyal
    Danyal over 3 years
    This worked but this also reinstalled my Python3. Which means it removed all the packages I had installed with it. Thanks though.
  • MrTomRod
    MrTomRod over 3 years
    This is how I did it: cd ~ - wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a‌​44a0a552c673a05f1101‌​8459c9f5faae3becc/Fo‌​rmula/[email protected] - brew install [email protected]
  • Nathan Loyer
    Nathan Loyer over 3 years
    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
    Nathan Loyer over 3 years
    @MrTomRod Your solution worked for me. The real answer is always in the comments.
  • hyperknot
    hyperknot over 3 years
    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.
  • Wayne
    Wayne over 3 years
    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 [email protected] After that, my old python code worked again. Thanks to all above.
  • Dewey
    Dewey over 3 years
    seems like this no longer works on the M1 Mac ... some type of build error ...
  • Sagar Ranglani
    Sagar Ranglani about 3 years
    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
  • nobled
    nobled almost 3 years
    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.
  • P A N
    P A N over 2 years
    These instructions don't work for me unfortunately. When attempting brew extract python@2 <user>/homebrew-python2, I get: Error: python@2: undefined method `[]' for nil:NilClass
  • P A N
    P A N over 2 years
    Where does virtualenv get installed using this method? I get command not found: virtualenv although it appears to have been installed somewhere.
  • Andreas Maier
    Andreas Maier over 2 years
    @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.
  • phette23
    phette23 about 2 years
    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
    phette23 about 2 years
    OK I downloaded from the Python releases page and the installer worked, though the script to install certificates does not. python.org/downloads/release/python-2716
  • Leon Shaner
    Leon Shaner about 2 years
    This worked the best for me after macOS 12.3 Monterey removed python 2.7.
  • scottalan
    scottalan about 2 years
    Thanks for the elegant solution and the link to the article
  • Borek Bernard
    Borek Bernard about 2 years
    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).
  • Captain Head
    Captain Head about 2 years
    @BorekBernard Yes, this answer completely omits the part where pyenv shims need to be added to your PATH PATH=$(pyenv root)/shims:$PATH See stackoverflow.com/a/71620699
  • Borek Bernard
    Borek Bernard about 2 years
    @CaptainHead Thanks! I've edited the answer, hope it's correct.
  • loongman
    loongman almost 2 years
    Got below error while execute brew install [email protected] Error: python@2: Invalid bottle tag symbol
  • Cruze
    Cruze almost 2 years
    very simple to execute. fix the issue on yarn build on node_sass