Oct 9, 2023 4 min read

How to Check Python Version

Check Python Version with our step-by-step tutorial. It is a popular high-level programming language renowned for its simplicity and versatility.

Check Python Version
Table of Contents

Introduction

Before we begin talking about how to check Python version, let's briefly understand – What is Python?

Python is a popular high-level programming language renowned for its simplicity and versatility. It emphasizes readable code, making it easy to learn and use for both beginners and experienced developers. Python supports a wide range of applications, including web development, data analysis, artificial intelligence, and automation.

Its vast collection of libraries and frameworks enhances productivity, whilst its cross-platform compatibility allows seamless execution on multiple operating systems. Python's straightforward syntax and extensive support community make it an excellent choice for anyone seeking a powerful and accessible programming language.

In this tutorial, you will check Python version. We will also address a few FAQs on how to check Python version.

Advantages of Python

  1. Simplicity: Python's clean and readable syntax makes it easy to learn and use.
  2. Versatility: Python can be used for various tasks, from web development to data analysis and artificial intelligence.
  3. Extensive libraries: Python offers a vast collection of libraries and frameworks, enhancing productivity and reducing development time.
  4. Cross-platform compatibility: Python programs can run on different operating systems without any modifications.
  5. Supportive community: Python has an active and helpful community, providing assistance and resources for developers at all levels.

Python Versioning

Python uses semantic versioning. Production-ready releases are versioned in the following scheme:

MAJOR.MINOR.MICRO

In Python 3.6.8, for example, 3 is a major version, 6 is a minor version, and 8 is a micro version.

  • MAJOR - Python has two major versions that are incompatible with one another: Python 2 and Python 3. 3.5.7, 3.7.2, and 3.8.0, for example, are all part of the Python 3 major version.
  • MINOR - These updates include 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 include a number of bug fixes and enhancements.

There are additional qualifiers for development releases. Read the Python "Development Cycle" documentation for more information.

Checking Python Version

Python comes standard with most Linux distributions and macOS. You must download and install it on Windows.

To find out which version of Python is installed on your system, run the python --version or python -V command:

python --version

The command will print the default Python version, in this case, that is 2.7.15. The version installed on your system may be different.

Output

Python 2.7.15+

All scripts with /usr/bin/python set as an interpreter in the script's shebang line will use the default version of Python.

In some Linux distributions, multiple versions of Python are installed at the same time. The Python 3 binary is commonly named python3, and the Python 2 binary is sometimes called python or python2.

You may check whether Python 3 is installed by typing:

python3 --version
Output

Python 3.6.8

Python 2 support will be discontinued in 2020. Python 3 is the language's present and future.

At the time of writing this tutorial, the most recent major release of Python is version 3.8.x. You most likely have an older version of Python 3 installed on your system.

The procedure for installing the most recent version of Python depends on the operating system you are using.

Programmatically Checking Python Version

Python 2 and Python 3 are fundamentally different. Python 2.x code may not work with Python 3.x code.

The sys module, which is available in all Python versions, provides system-specific parameters and functions. The sys.version_info function returns the Python version installed on the system. It returns a tuple containing the following five version numbers: major, minor, micro, release level, and serial.


Assume you have a script that requires at least Python version 3.5, and33 you want to see if your system meets the requirements. You can accomplish this by simply comparing the major and minor versions:

import sys

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

If you run the script with a Python version less than 3.5, you will get the following results:

Output

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

Use the future module to write Python code that works in both Python 3 and Python 2. It enables the execution of Python 3.x-compatible code under Python 2.

FAQs to Check Python Version

What is the difference between Python 2 and Python 3? 

Python 2 and Python 3 are two different major versions of Python. Python 3 is the latest version and offers improved features, syntax, and support.

Can I have both Python 2 and Python 3 versions installed on my computer? 

Yes, it is possible to install and use multiple versions of Python on the same computer. Ensure that the correct version is invoked when executing a script.

How can I upgrade my Python version? 

You can download and install the latest Python version from the official Python website (python.org). Follow the installation instructions to upgrade your Python version.

Are Python versions backward compatible? 

Not entirely. Python 2 and Python 3 have some differences in syntax and features, making code written for one version not always compatible with the other.

How can I update pip (Python package manager)? 

To update pip, open your command prompt or terminal and enter the command python -m pip install --upgrade pip. This will upgrade pip to the latest version.

Can I switch between different Python versions?

 Yes, you can use tools like pyenv or conda to manage multiple Python versions on your system and easily switch between them.

What Python version should I use for my project? 

It depends on your requirements and dependencies. Python 3 is recommended for most projects, as Python 2 has reached its end of life and is no longer maintained with updates and security fixes.

Conclusion

To find out what version of Python is installed on your system, simply type python --version.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.