How can I install a previous version of Python 3 in macOS using homebrew? How can I install a previous version of Python 3 in macOS using homebrew? python python

How can I install a previous version of Python 3 in macOS using homebrew?


Short Answer

To make a clean install of Python 3.6.5 use:

brew unlink python # ONLY if you have installed (with brew) another version of python 3brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

If you prefer to recover a previously installed version, then:

brew info python           # To see what you have previously installedbrew switch python 3.x.x_x # Ex. 3.6.5_1

Long Answer

There are two formulas for installing Python with Homebrew: python@2 and python.
The first is for Python 2 and the second for Python 3.

Note: You can find outdated answers on the web where it is mentioned python3 as the formula name for installing Python version 3. Now it's just python!

By default, with these formulas you can install the latest version of the corresponding major version of Python. So, you cannot directly install a minor version like 3.6.

Solution

With brew, you can install a package using the address of the formula, for example in a git repository.

brew install https://the/address/to/the/formula/FORMULA_NAME.rb

Or specifically for Python 3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/COMMIT_IDENTIFIER/Formula/python.rb

The address you must specify is the address to the last commit of the formula (python.rb) for the desired version.You can find the commint identifier by looking at the history for homebrew-core/Formula/python.rb

https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb

Python > 3.6.5

In the link above you will not find a formula for a version of Python above 3.6.5.After the maintainers of that (official) repository released Python 3.7, they only submit updates to the recipe of Python 3.7.

As explained above, with homebrew you have only Python 2 (python@2) and Python 3 (python), there is no explicit formula for Python 3.6.

Although those minor updates are mostly irrelevant in most cases and for most users, I will search if someone has done an explicit formula for 3.6.


As an update, when doing

brew unlink python # If you have installed (with brew) another version of pythonbrew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

You may encounter

Error: python contains a recursive dependency on itself:  python depends on sphinx-doc  sphinx-doc depends on python

To bypass it, add the --ignore-dependencies argument to brew install.

brew unlink python # If you have installed (with brew) another version of pythonbrew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb


What I did was first I installed python 3.7 and then unlinked it using the following commands

brew install python3brew unlink python

then I installed python 3.6.5 using the following command taken from above answer.

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb --ignore-dependencies

After that I ran the following command

brew link --overwrite python

Now I have all pythons in the system. To find out run

mian@tdowrick2~ $ python --versionPython 2.7.10mian@tdowrick2~ $ python3.7 --versionPython 3.7.1mian@tdowrick2~ $ python3.6 --versionPython 3.6.5

To create Python 3.7 virtual environment.

mian@tdowrick2~ $ virtualenv -p python3.7 envAlready using interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'New python executable in /Users/mian/env/bin/python3.7Also creating executable in /Users/mian/env/bin/pythonInstalling setuptools, pip, wheel...done.mian@tdowrick2~ $ source env/bin/activate(env) mian@tdowrick2~ $ python --versionPython 3.7.1(env) mian@tdowrick2~ $ deactivate

To create Python 3.6 virtual environment

mian@tdowrick2~ $ virtualenv -p python3.6 envRunning virtualenv with interpreter /usr/local/bin/python3.6Using base prefix '/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6'New python executable in /Users/mian/env/bin/python3.6Not overwriting existing python script /Users/mian/env/bin/python (you must use /Users/mian/env/bin/python3.6)Installing setuptools, pip, wheel...done.mian@tdowrick2~ $ source env/bin/activate(env) mian@tdowrick2~ $ python --versionPython 3.6.5(env) mian@tdowrick2~ $ deactivate