How to Select Which Version of Python I am Running on Linux

Which version of Python do I have installed?

python -V

http://docs.python.org/using/cmdline.html#generic-options

--version may also work (introduced in version 2.5)

How do I check which version of Python is running my script?

This information is available in the sys.version string in the sys module:

>>> import sys

Human readable:

>>> print(sys.version)  # parentheses necessary in python 3.       
2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]

For further processing, use sys.version_info or sys.hexversion:

>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192

To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:

assert sys.version_info >= (2, 5)

This compares major and minor version information. Add micro (=0, 1, etc) and even releaselevel (='alpha','final', etc) to the tuple as you like. Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). Sometimes features go away in newer releases, being replaced by others.

How to switch Python versions in Terminal?

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.

How to ensure I use the correct python version when I have multiple versions of Python installed?

Having multiple versions on the same machine is perfectly "okay", as long as you understand how to select/use the correct python version, which is the more pertinent and useful question you should be asking yourself.

They would normally be installed in separate locations and would have separate site-packages (from How do I find the location of my Python site-packages directory?):

~$ python3.7 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

~$ python3.8 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']

~$ python3.9 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']

The recommended way however is to not install to these site-packages folder directly and use a virtual environment instead. There are many variations of virtual env packages/tools, but you can start with the Python docs on Virtual Environments if you are not familiar or using one yet.

With virtual environments, you can usually indicate which version of python to use when creating the virtual env, so that whenever you activate that same env, it would use the same version you used to create it.

tmp$ python3.7 -m venv app1
tmp$ source ./app1/bin/activate
(app1) tmp$ python -V
Python 3.7.12
(app1) tmp$
(app1) tmp$ deactivate

tmp$ python3.8 -m venv app2
tmp$ source ./app2/bin/activate
(app2) tmp$ python -V
Python 3.8.12
(app2) tmp$
(app2) tmp$ deactivate

tmp$ python3.9 -m venv app3
tmp$ source ./app3/bin/activate
(app3) tmp$ python -V
Python 3.9.9
(app3) tmp$
(app3) tmp$ deactivate

In the above example, I created 3 virtual environments for 3 hypothetical apps, each using a different Python version. As long as I activate the correct virtual environment, I don't have to think about which version python refers to, as it will use the same version used to create the env. See also How do I check what version of Python is running my script?.

As for installing packages, again, once you have setup the correct virtual environments, doing pip install would ensure it installs only on the site-packages on that environment, and the app running on that env would be able to import that package.

If not using a virtual environment, as I noted in my comment, the answers at Dealing with multiple Python versions and PIP? provide good suggestions on how to ensure you are installing packages for the correct Python version, namely with

$ </path/or/alias/to/specific/python/installation> -m pip install <packages>
$ python3.7 -m pip install "flake8<=3.6"
$ python3.7 -m pip list | grep flake8
flake8 3.6.0
$ ls -H /usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages | grep flake8
flake8
flake8-3.6.0.dist-info
flake8_quotes
flake8_quotes-3.2.0.dist-info

$ python3.9 -m pip install flake8
$ python3.9 -m pip list | grep flake8
flake8 4.0.1
$ ls -H /usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages | grep flake8
flake8
flake8-4.0.1.dist-info

The above example shows 2 different versions of flake8 installed for 2 different versions of Python, each on their own site-packages folder.

But when I run my command prompt and check my python version it says I am using the recent version. The 3.10.0.

This means the system default for python or python3 on your machine points to the 3.10 installation. Mine is set to point to Python 3.9:

tmp$ python3 -V
Python 3.9.9
tmp$ which python3
/usr/local/bin/python3
tmp$ /usr/local/bin/python3 -V
Python 3.9.9

You can:

  1. Manually change the default version
  2. Setup aliases to the different versions:
    tmp$ type python3.7
    python3.7 is aliased to `/usr/local/opt/python@3.7/bin/python3'

    tmp$ type python3.8
    python3.8 is aliased to `/usr/local/opt/python@3.8/bin/python3'

    tmp$ type python3.9
    python3.9 is aliased to `/usr/local/opt/python@3.9/bin/python3'
  3. Use a version management tool for switching versions, such as pyenv

How to change python version in command prompt if I have 2 python version installed

Both Python2 and Python3 is installed
As you can see, I have both Python2 and Python3 installed.

Windows Path variable
I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.

How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM.
Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.

Both Python2 and Python3 executables have the same name in Windows so it never runs python3.

What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.

How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.

.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.

Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.

python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.

Python and Python3
python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.

I hope this solves your problem.

How do I tell a Python script to use a particular version

You can add a shebang line the to the top of the script:

#!/usr/bin/env python2.7

But that will only work when executing as ./my_program.py.

If you execute as python my_program.py, then the whatever Python version that which python returns will be used.

In re: to virtualenv use: virtualenv -p /usr/bin/python3.2 or whatever to set it up to use that Python executable.

Unable to set default python version to python3 in ubuntu

EDIT:

I wrote this when I was young and naive, update-alternatives is the better way to do this. See @Pardhu's answer.


Outdated answer:

Open your .bashrc file nano ~/.bashrc. Type alias python=python3
on to a new line at the top of the file then save the file with ctrl+o
and close the file with ctrl+x. Then, back at your command line type
source ~/.bashrc. Now your alias should be permanent.



Related Topics



Leave a reply



Submit