How to switch Python versions in Terminal?

My Mac came with Python 2.7 installed by default, but I'd like to use Python 3.6.1 instead.

How can I change the Python version used in Terminal (on Mac OS)?

Please explain clearly and offer no third party version manager suggestions.

3

10 Answers

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.

4

As Inian suggested, you should alias python to point to python 3. It is very easy to do, and very easy to switchback, personally i have an alias setup for p2=python2 and p3=python3 as well to save on keystrokes. Read here for more information: How do I create a Bash alias?

Here is an example of doing so for python:

alias python=python3 

Like so:

$ python --version Python 2.7.6 $ python3 --version Python 3.4.3 $ alias python=python3 $ python --version Python 3.4.3 

See here for the original:

1

You can just specify the python version when running a program:

for python 2:

python filename.py 

for python 3:

python3 filename.py 

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.

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... -> 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) $ mkdir pyenv-experiment && echo "3.8.1" > "pyenv-experiment/.python-version" $ 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) 

If you have python various versions of python installed,you can launch any of them using pythonx.x.x where x.x.x represents your versions.

You can open and use zsh terminal on Mac OS.

Edit file /Users/{your_username}/.zshrc using nano or vim.

Add new alias for python 3

alias python="python3" 

Save and check your python version using this following command.

python --version 

Look at the result:

enter image description here

1

Here is a nice and simple way to do it (but on CENTOS), without braking the operating system.

yum install scl-utils 

next

yum install centos-release-scl-rh 

And lastly you install the version that you want, lets say python3.5

yum install rh-python35 

And lastly:

scl enable rh-python35 bash 

Since MAC-OS is a unix operating system, the way to do it it should be quite similar.

I have followed the below steps in Macbook.

  1. Open terminal
  2. type nano ~/.bash_profile and enter
  3. Now add the line alias python=python3
  4. Press CTRL + o to save it.
  5. It will prompt for file name Just hit enter and then press CTRL + x.
  6. Now check python version by using the command : python --version

I am a beginner in python and was looking for the same and in the terminal, I just typed python3 and it came up with the newest version. I am thinking that if one wants to go to a different version they can just type that in? Could be wrong but this is what shows up when I typed python3.

% python3 Python 3.9.2 (v3.9.2:1a79785e3e, Feb 19 2021, 09:06:10) [Clang 6.0 (clang-600.0.57)] on darwin 

Before when I just typed python. This is the message I would get.

% python2.7 WARNING: Python 2.7 is not recommended. This version is included in macOS for compatibility with legacy software. Future versions of macOS will not include Python 2.7. Instead, it is recommended that you transition to using 'python3' from within Terminal. Python 2.7.16 (default, Jun 5 2020, 22:59:21) [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin Type "help", "copyright", "credits" or "license" for more information. 
0

In order to easily manage the different python versions. Please use below link to see how to use the versions effectively and without any environment variables.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like