• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Check Python Version

Written by Admin, Updated On September 12, 2020
python, terminal
Check Python Version

Python is one of the popular programming language. It is used for machine learning, analyzing data, websites and more. Sometimes, for developing it’s required to determine version of Python whether it is supported or not. In this article we will show you how to check the version of Python is installed on your system.

Python Version#

There are different versions of Python, but the two most popular ones are Python 2.7.x and Python 3.7.x.

Python uses semantic versioning. When looking at the version number, there are usually three digits to read:

MAJOR.MINOR.MICRO

For example, in Python 3.7.2, 3 is major version, 7 is a minor version, and 2 is a micro version.

  • MAJOR – Python 2 and Python 3 are major versions that are not fully compatible. For instance, 3.5.7, 3.7.2, and 3.8.0 are all part of the Python 3 major version.
  • MINOR – These releases are with new features and functions. For example, 3.6.6, 3.6.7, and 3.6.8 are all part of the Python 3.6 minor version.
  • MICRO – The new micro versions contain various bug fixes and improvements.

How to Check Python Version in Linux#

Currently, most modern Linux distributions and macOS comes with Python pre-installed. For Windows, you should download and install it.

To check which version is installed on your system, open terminal on your system and run the python --version or python -V command:

python --version

In output, it will print default Python version of your system. On this system it’s 3.7.8. It may be different version on your system.

For development the default version of Python will be used. Sometimes multiple Python versions are installed on a single system. If you have Python 3 installed you can check by typing:

python3 --version
Python 3.7.8

Python 2 support ends in 2020. Python 3 is the present and future of the language.

At the time of writing this article, the latest major release of the Python is version 3.8.x. Chances are that you have an older version of Python 3 installed on your system.

Check Python Version using Script#

Software that’s written in one version often will not work correctly in another version. When using Python, it is essential to know which version an application requires, and which version you have.

Generally, it is recommended to have the software check the version of Python before it runs to prevent crashes and incompatibilities.

Let’s make a script to check the version that requires at least Python version 3.7, and you want to check whether the system meets requirements. You can do that by simply checking the major and minor versions:

Use the following code snippet to check for the correct version of Python:

import sys

if not (sys.version_info.major == 3 and sys.version_info.minor >= 7):
    print("This script requires Python 3.7 or higher!")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
    sys.exit(1)

When the script runs, it will first check for the version of Python and if the version is less than 3.7 it will show following output:

This script requires Python 3.7 or higher!
You are using Python 2.7.

Conclusion#

In this article explained how to check the Python version.

If you have any question or suggestion, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Sublime Text 3 on Ubuntu 20.04
Next Article   How to Install Tor Browser on Ubuntu 20.04

Related Posts

  • How to Install Python 3.11 on Debian 11

    How to Install Python on Debian 11

    January 25, 2023
  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook