I have done this sort of thing in two ways - however both do not coexist with homebrew although I suspect there should be a way similar to the macports one.
The first is using Macports as the package manager. There are separate ports for several python versions 2.x and 3.x currently 2.4, 2.5, 2.6, 2.7 and 3.1, 3.2, 3.3 3.4 and 3.5. For example to install 3.4 do There is also a python_select port that running it allows you to choose which version of python is run by /opt/bin/python. e,g, to select macports python 2.7 sudo port install python34port select --set python python27
Each package manager needs to run as the only package manager otherwise it will be confused with unexpected different versions of libraries and executables on its paths.
From comments the similar homebrew way is given in https://stackoverflow.com/a/4158763/151019 Short answer seems to be the equivalent to port_selectbrew switch
The other way is install direct from python.org This will install a python into /usr/local/bin I think this installs a python and python3 executable. The issue with Homebrew is that it assumes that things in /usr/local are installed by it and so brew doctor will report these python.org pythons and libraries as issues and as this question shows also the frameworks in /Library will be problematic.
EDIT 8/2018 *** I would now suggest using conda either miniconda or anaconda. This is another package manager but based and mainly meant for python, it also gives the equivalent of python's virtual environments and pyenv (as per other answer) and so each project can be in a different python and different libraries (Macports only switches python version for all projects)
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 3
brew 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 installed
brew switch python 3.x.x_x # Ex. 3.6.5_1
There are two formulas for installing Python with Homebrew: and python@2.python
Note: You can find outdated answers on the web where it is mentioned as the formula name for installing Python version 3. Now it's just python3!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.
With , you can install a package using the address of the formula, for example in a git repository.brew
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
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.
Felix Krull runs a PPA offering basically any version of Python (seriously, there is 2.3.7 build for vivid...) for many Ubuntu releases at https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa.
Do the usual:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5
It will not overwrite your existing which is still symlinked as python3.4.python3
Instead, to run python3.5, run the command (or python3.5 for any other version of python).python3.X
DON'T change the symlink! There are apparently many system functions that don't work properly with python3.5.
I tried this and afterwards couldn't open a terminal, software updater,...
cd /usr/bin
sudo rm python3
The upgrade to Wily will adapt the meta-package to point to python3. I don't expect any breakage, but at this point the foreign repository is not needed anymore. So to be really safe, you can purge the PPA before doing the upgrade.python3.5
There is an Homebrew known issue related to side by side install of Python 3.8 / 3.9. To workaround, following commands should work for you:
brew unlink python@3.9
brew unlink python@3.8
brew link --force python@3.9
Re-opening your terminal or execute command can be required to take account the change.rehash
Homebrew doesn't do this, I would recommend using homebrew to install pyenv and then using pyenv to install and use python 2.7.10, and any other versions you need.
Get pyenv:
brew update
brew install pyenv
Then add to your eval "$(pyenv init -)" and relaunch terminal..bash_profile
Install python 2.7.10:
pyenv install 2.7.10
You can then set 2.7.10 as the global python by using but I would instead recommend you look at the pyenv virtualenv or pyenv virtualenvwrapper projects and use a python virtual environment for your code, or set the python for your project folder only by pyenv global 2.7.10'ing to your project folder and using cd.pyenv local 2.7.10
If versions on are defined right, you must also be able to homebrew/code for example.brew install node@0.12
You can also install multiple versions and select which one you want to use with the command.brew switch
--
Anyway, I'd recommend using nvm, which can be installed through Homebrew. Although, the version on is buggy and they don't plan fixing it.brew