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.
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.
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)
Switch to a specific version of Python on macOS
Recommended way to manage several installed versions of Python (macOS)
How can I switch default python version on command line?
I installed python 3.10 on my Mac but when I enter “python —version” it returns 3.8.3. How do I fix this?
Videos
The simplest way would be to add an alias to python3 to always point to the native python installed. Add this line to the .bash_profile file in your $HOME directory at the last,
alias python="python3"
Doing so makes the changes to be reflected on every interactive shell opened.
pyenv is a 3rd party version manager which is super commonly used (18k stars, 1.6k forks) and exactly what I looked for when I came to this question.
edit: I use it for several years now. Works like a charm.
Installation
Install pyenv.
Usage
$ pyenv install --list
Available versions:
2.1.3
[...]
3.8.1
3.9-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
[... a lot more; including anaconda, miniconda, activepython, ironpython, pypy, stackless, ....]
$ pyenv install 3.8.1
Downloading Python-3.8.1.tar.xz...
-> https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
Installing Python-3.8.1...
Installed Python-3.8.1 to /home/moose/.pyenv/versions/3.8.1
$ pyenv versions
* system (set by /home/moose/.pyenv/version)
2.7.16
3.5.7
3.6.9
3.7.4
3.8-dev
$ python --version
Python 2.7.17
$ pip --version
pip 19.3.1 from /home/moose/.local/lib/python3.6/site-packages/pip (python 3.6)
# Writing the .python-version file does the switch:
$ mkdir pyenv-experiment && echo "3.8.1" > "pyenv-experiment/.python-version"
# alternatively, you could use `pyenv local 3.8.1`
$ cd pyenv-experiment
$ python --version
Python 3.8.1
$ pip --version
pip 19.2.3 from /home/moose/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)
When I use VS Code and select a version of Python on macOS, I have the following versions:
-
Python 3.12.8 ('3.12.8') ~/.pyenv/versions/3.12.8/bin/python
-
Python 3.13.2 /opt/homebrew/bin/python
-
Python 3.12.8 /usr/local/bin/python3
-
Python 3.9.6 /Library/Developer/CommandLineTools/usr/bin/python3
-
Python 3.9.6 /usr/bin/python3
I believe having this many versions of Python in different locations messes me up when trying to install packages (i.e. using brew vs pip3 vs pyenv), so I'm wondering what the best way is to clean this up and make package + version management easier?
I have macOS and downloaded VSCode which I think comes with Python 3.12. Then, I downloaded Python 3.8 from the official website. Now, Python 3.8 is the default version. How can I switch default to Python 3.12 without deleting 3.8? Thanks in advance!
I’m trying to run python on my Mac for a project and it seems the version is wrong. I tried uninstalling then reinstalling and it is still doing it. I need it so that I can use pip to download py.serial.
Changing the default python executable's version system-wide could break some applications that depend on python2.
However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put
alias python='python3'
in your ~/.profile, and then source ~/.profile in your ~/.bash_profile and/or your~/.zsh_profile with a line like:
[ -e ~/.profile ] && . ~/.profile
This way, your alias will work across shells.
With this, python command now invokes python3. If you want to invoke the "original" python (that refers to python2) on occasion, you can use command python, which will leaving the alias untouched, and works in all shells.
If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:
alias 2='python2'
alias 3='python3'
Tip: For scripts, instead of using a shebang like:
#!/usr/bin/env python
use:
#!/usr/bin/env python3
This way, the system will use python3 for running python executables.
You can solve it by symbolic link.
unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python