Switch between python 2.7 and python 3.5 on Mac OS X
Solution 1:
IMHO, the best way to use two different Python versions on macOS is via homebrew
. After installing homebrew on macOS, run the commands below on your terminal.
brew install python@2
brew install python
Now you can run Python 2.7 by invoking python2
or Python 3 by invoking python3
. In addition to this, you can use virtualenv
or pyenv to manage different versions of python
environments.
I have never personally used miniconda
but from the documentation, it looks like it is similar to using pip
and virtualenv
in combination.
Solution 2:
OSX's Python binary (version 2) is located at /usr/bin/python
if you use which python
it will tell you where the python
command is being resolved to. Typically, what happens is third parties redefine things in /usr/local/bin
(which takes precedence, by default over /usr/bin
). To fix, you can either run /usr/bin/python
directly to use 2.x or find the errant redefinition (probably in /usr/local/bin
or somewhere else in your PATH
)
Solution 3:
I already had python3 installed(via miniconda3) and needed to install python2 alongside in that case brew install python
won't install python2, so you would need
brew install python@2
.
Now alias python2
refers to python2.x from /usr/bin/python
and alias python3
refers to python3.x from /Users/ishandutta2007/miniconda3/bin/python
and alias python
refers to python3 by default.
Now to use python
as alias for python2, I added the following to .bashrc
file
alias python='/usr/bin/python'
.
To go back to python3 as default just remove this line when required.
Solution 4:
How to set the python version back to 2.7 if you have installed Anaconda3 (Python 3.6) on MacOS High Sierra 10.13.5
Edit the .bash_profile file in your home directory.
vi $HOME/.bash_profile
hash out the line # export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
Close the shell open again you should see 2.7 when you run python.
Then if you want 3.6 you can simply uncomment your anaconda3 line in your bash profile.
Trying to unlink python will end in tears in Mac OSX.
You will something like this
unlink: /usr/bin/python: Operation not permitted
Hope that helps someone out !! :) :)