Objective
This article explains how to switch between Python2 and Python3 on Debian 9 Stretch LinuxOperating System and Software Versions
- Operating System: - Debian 9 Stretch
Requirements
Privileged access to to your Debian Linux installation will be required.Difficulty
EASYConventions
- # - requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $ - requires given linux commands to be executed as a regular non-privileged user
Instructions
Debian 9 Stretch comes with two2.7
and 3.5
python versions. If you have not installed any python package yet you can choose between both by simply installing an appropriate package:PYTHON 2 INSTALLATION: # apt install python PYTHON 3 INSTALLATION: # apt install python3
Check Default Python Version
To check a default python version simply runpython
command and query its version: $ python --version Python 2.7.13
Install Python
Let's assume that no python interpreter is not yet installed on our system. Hence, we can start by installation of both python versions:# python --version -bash: python: command not found # apt install python python3After installation the Python version
2.7
is made default: $ python --version Python 2.7.13
Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
Update Python ALternatives List
To perform a system-wide switch between default python versions useupdate-alternatives
command. At first the update-alternatives
command will complain that there are no python alternatives available: # update-alternatives --list python update-alternatives: error: no alternatives for pythonTo install Python alternatives, first list all available options:
$ ls /usr/bin/python* /usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python3mNext, update the Python alternatives list for each version you whish to use. In our case with stick with
/usr/bin/python2.7
and /usr/bin/python3.5
versions: # update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode # update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2 update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto modePlease note that the integer number at the end of each command denotes a priority. Higher number means higher priority and as such the
/usr/bin/python3.5
version was set in Auto Mode to be a default if no other selection is selected. After executing both above commands your current default python version is /usr/bin/python3.5
due to its higher priority (2
): # python --version Python 3.5.3
Switch Between Python Versions
Now, that we have updated list of Python alternatives to perform a switch between any python version is to run:# update-alternatives --config python There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.5 2 auto mode 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.5 2 manual mode Press <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual modeand select an appropriate version using selction integer as shown above.
# python --version Python 2.7.13
Local User Python Version
In case you need to only change a python version selectively on per user basis, you may try to edit user's.bashrc
file. For example to change to python version 3.5
execute the following linux commands: $ python --version Python 2.7.13 $ echo 'alias python="/usr/bin/python3.5"' >> ~/.bashrc $ . .bashrc $ python --version Python 3.5.3
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions