How to reinstall Python 2 using Homebrew?

How to Brew Install Python2?

I am facing issues with openssl and python@2 in Homebrew. When I try to reinstall Python 2 using:

brew install python@2

I get this error:

Error: No available formula with the name "python@2"
python@2 was deleted from homebrew/core in commit 028f11f9e. EOL 1 January 2020.

Some brew formulas like awscli, letsencrypt, and sshuttle still depend on Python 2. After reinstalling awscli, running commands like aws gives errors due to the missing Python 2 interpreter.

How can I reinstall Python 2 with Homebrew or use the brew extract method to maintain it?

I’ve worked with Homebrew quite a bit and here’s a straightforward way to bring back Python 2 using Homebrew itself.

Use brew extract to Restore Python 2 You can use the brew extract command to restore the deleted formula for Python 2 and maintain it in your own tap:

  1. Create a custom tap:
brew tap myuser/homebrew-python  
  1. Extract the Python 2 formula:
brew extract --version=2.7.18 python@2 myuser/homebrew-python  
  1. Install Python 2 from your tap:
brew install myuser/homebrew-python/python@2  

This method allows you to maintain the Python 2 formula locally. It’s ideal if you need to control dependencies for tools like brew install python2.

@panchal_archanaa’s solution works great, but if you want more flexibility managing multiple Python versions, here’s another approach I’ve been using."*

Use Pyenv for Python Version Management Rather than relying entirely on Homebrew, Pyenv can help manage Python versions, including Python 2.

  1. Install Pyenv:
brew install pyenv  
  1. Install Python 2:
pyenv install 2.7.18  
  1. Set Python 2 as the global or local version:
pyenv global 2.7.18  

This way, you can switch between Python versions as needed, while still installing dependencies with brew install python2 when required. Pyenv provides better version isolation, especially useful for tools like awscli.