How to Check Python Version in Linux, Mac, & Windows

December 15, 2023

Introduction

Python is an object-oriented programming language used for web development, machine learning, and automation tasks. It comes in different versions that differ in compatibility, support lifecycle, performance, security, and features.

Certain applications may require a specific version of Python, so it is crucial to know which program version you have on your system and, if necessary, install a specific one.

In this tutorial, learn how to check the Python version on Windows, Linux, or macOS.

How to check Python version - a tutorial.

Prerequisites  

Which Python Versions Exist?

The primary Python branches are Python 2 and Python 3, and each has several versions. The latest version at the time this article was written was Python 3.12.1. Python 2's final version is 2.7, which reached its end of life on January 1, 2020.

Python 2 is no longer supported, and users are strongly encouraged to migrate their code to Python 3 to ensure ongoing support and compatibility with the latest features and improvements.

When looking at the version number, there are usually three digits to read:

1. The major version - Python 3.12.1.

2. The minor version - Python 3.12.1.

3. The micro version - Python 3.12.1.

While major releases are not fully compatible, minor releases generally are. For example, version 3.12.1 should be compatible with 3.12.2. The final digit signifies the latest patches and updates.

Python 2.7 and 3.7 are different applications. Software written in one version often does not work correctly in another version. When using Python, it is essential to know which version an application requires and which version you have.

Note: Read our R vs Python comparison to learn more about these two popular programming languages in visualization, data science, and data analysis.

How to Check Python Version in Linux

Most modern Linux distributions come with Python preinstalled. To check which version is installed, open a terminal window and run the following command:

python3 --version
Checking the Python version in Linux Ubuntu.

Since most Linux versions now use Python 3 by default, we use python3 in the command syntax. However, if you still use Python 2 for some applications, omit 3 from the command to check which Python 2 version you have installed.

How to Check Python Version in Windows

Windows installations do not come with Python preinstalled. However, if you have been using Python earlier but don't remember which version, you can easily check which one is installed.

Open Windows Powershell or the Command Prompt, and run the following command:

python --version

If you have Python installed, Powershell reports the version number.

Checking the Python version in Windows.

How to Check Python Version in MacOS

If you are using a Mac, check the Python version by running the following command in the terminal:

python3 --version
Checking the Python version in macOS.

The system reports the installed version.

Checking a System with Multiple Python Versions

Python 2 and Python 3 are different programs, allowing users to run Python 2 installations separately from Python 3 on the same system. Python 3 is not entirely backward compatible.

Most systems differentiate Python 2 as python and Python 3 as python3. If you do not have Python 2, your system may use the python command in place of python3, which can be seen from the Windows example.

However, in Linux and macOS, if you need to check Python 2 version, enter:

python --version

To check the version of Python 3 software:

python3 --version

Note: Python does not have a built-in upgrade system. You need to download the latest version and install it.

How to Check Python Version in Script

When writing an application, include a mechanism that checks the Python version before execution, as this helps prevent crashes and resolve potential incompatibilities. Follow the steps below to create a Python script that checks the program version:

1. Open a text editor and paste the following code:

import sys
if not sys.version_info.major == 3 and sys.version_info.minor >= 10:
    print("Python 3.10 or higher is required.")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
    sys.exit(1)
else:
    print("You're good to go!")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))

You can replace the version number with your own prerequisite.

2. Save the script with a .py extension and run it with python3. For example:

Checking the Python version using a Python script.

When the script runs, it tests to see if Python 3.10 is installed. After testing, it sends a notification and displays the current Python version.

Note: One of the common issues in working with Python and datasets is missing data. Learn how to handle missing data in Python.

Conclusion

You now know how to check the Python version in several operating systems. Checking the Python version is crucial for ensuring compatibility with specific libraries and functionalities.

If you want to learn how to upgrade Python to a newer version on Windows, macOS, and Linux, check out our article on how to upgrade Python.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Use Comments in Python
November 25, 2019

The ability to use comments while writing code is an important skill valued among developers. These comments ...
Read more
How to Get the Current Date and Time in Python
March 12, 2024

The article shows you how to create a basic Python script that displays the current date and time. Find out ...
Read more
How To Install Python 3 on Windows 10
December 5, 2023

Unlike most Linux distributions, Windows does not come with the Python programming language by default.
Read more
How To Install PIP to Manage Python Packages On Windows
November 30, 2023

PIP for Python is a utility to manage PyPI package installations from the command line. This tutorial will ...
Read more