Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How to Switch Python Version in Ubuntu & Debian

    How to Switch Python Version in Ubuntu & Debian

    RahulBy RahulOctober 5, 20213 Mins ReadUpdated:April 22, 2022

    Python is a high-level programming language, widely used for system programming. It is available for all popular operating systems. You can install more than one Python version on a single system. Once you have installed multiple Python versions, you can switch the default Python with the update-alternatives tool.

    All the Python developers are recommended to use a virtual environment for the applications. Which provides an isolated environment for the application with a defined Python version.

    Switch Python Version on Ubuntu & Debian

    The update-alternatives command-line tool is to create and maintain symbolic links for the default commands. With the help of this, we can easily switch commands to different versions. For, this tutorial, Python 3.10, and Python 2.7 are pre-installed on a Debian system. We will create a group for both commands and set symbolic links.

    1. Create a symlink from /usr/bin/python2.7 to /usr/bin/python and set the group name as “python”. Later, group name will be used to switch links.
      sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 
      
      Output
      update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
    2. Change the symlink link to /usr/bin/python3.10 for /usr/bin/python and set the group name to “python”. The group name must be same for all python versions.
      sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2 
      
      Output
      update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python (python) in auto mode
    3. Repeat step 2 to add more Python version to group, which is already installed on your system.
    4. At this point, You have added two python binary versions to the group name “python”. Now, you can easily switch to any version with the following commands. Here “python” is the group name defined in the above commands.
      sudo update-alternatives --config python 
      
      Output: [Select on option]
      There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.10 2 auto mode * 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.10 2 manual mode Press to keep the current choice[*], or type selection number:

      In this above output Python, 2.7 is set as the current version. To change this to Python 3.10, you need to input 0 or 2 and hit enter.

    5. That’s it. The current Python version is changed on your system. Just type the following command to view the correctly active Python version.
      python -V 
      
      Output
      Python 3.10.2

      You can add multiple Python versions to a group (Steps: 01 & 02) and easily switch between them.

    Conclusion

    In this tutorial, you have learned about switching the default Python versions on your Ubuntu and Debian Linux systems. Instead of switching version you can also configure Python virtual environment for your applications.

    Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleWorking with Python Functions
    Next Article Things To Do After Installing Linux Mint 20 “Ulyana”

    Related Posts

    How to accept user input in Python

    2 Mins Read

    What is the /etc/aliases file

    2 Mins Read

    What is the /etc/nsswitch.conf file in Linux

    2 Mins Read

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    Setup Selenium with Python and Chrome on Fedora

    Updated:June 18, 20223 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.