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
Similar to John Wilkey's answer I would run python2 by finding which python
, something like using /usr/bin/python
and then creating an alias in .bash_profile
:
alias python2="/usr/bin/python"
I can now run python3 by calling python
and python2 by calling python2
.
Solution 5
Here is 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
- Comment out the line with
anaconda3
addition to yourPATH
like this:# export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
- Close the shell, then open it again
- You should now see 2.7 when you run
python
Then, if you want 3.6 you can simply uncomment out the anaconda3
line in your .bash_profile.
Trying to unlink python
will end in tears in Mac OS X.
You will get something like this
unlink: /usr/bin/python: Operation not permitted
Comments
-
Viswanath about 2 years
I generally use Python 2.7 but recently installed Python 3.5 using Miniconda on Mac OS X. Different libraries have been installed for these two versions of python. Now, the entering either of the keywords 'python' or 'python3' in terminal invokes python 3.5, and 'python2' returns '-bash: python2: command not found'. How can I now invoke them specifically using aliases 'python2' and 'python3' respectively?
I am currently using OS X El Capitan.
-
TigerhawkT3 over 8 yearsHave you tried
python -2
? -
Emil Vikström over 8 yearsSounds like you should open a bug report with Miniconda that they are not following PEP-0394.
-
David Z over 8 yearsWhat do you mean by switching between them? Do you want to be able to invoke Python 2 specifically, when you want it, and invoke Python 3 specifically when you want that version? That you can do using the aliases
python2
andpython3
(and I'll post that as an answer if this is what you want and you edit your question accordingly). Or do you want to switch which version of Python actually runs when you type justpython
? -
Charlie Parker over 6 yearseven if your able to run
python2
you might need to re-install things for python 2 it seems...
-
-
Viswanath over 8 yearsBut, I'm looking for the way to create an alias, say
python2
to invoke Python 2 andpython3
to invoke Python 3. Could you give me some information on that direction? Thnx. -
Charlie Parker over 6 yearshow does one install things for python2 now?
-
forevergenin over 5 yearspip2 install <package_name>