How to Check Python Version – Windows, Mac, Linux

by | Python

Python is a versatile, powerful programming language with many applications, from web development to data analysis. Most often when programming in Python, you’ll want to know which version of Python you are using.

You can check your Python version quickly and effortlessly using multiple methods, imncluding command-line tools, running a script with the platform and sys modules, or within your preferred IDE.

These approaches are applicable across different operating systems, including Windows, macOS, and Linux, ensuring you can identify your Python version in no time.

Each new version of Python comes with certain changes or updates, therefore, code written previously in another version might not work in the newer versions.

Different versions of Python can have subtle differences and knowing the exact version you’re working with can help you avoid compatibility issues when sharing code with others or installing packages.

How to Check Python Version.

In this article, we’ll provide a straightforward guide on how to check the Python version on your computer, whether you’re using Windows, macOS, or Linux.

Let’s get into it!

How to Check Python Version from the Command Line

In this section, we’ll discuss how to check Python version using the command line. This method is quick and effective for determining which version of Python is installed on your computer.

Specifically, we will cover the following:

  1. How to check Python version in Windows

  2. How to check Python version in Linux

  3. How to check Python version in MacOS

Let’s get into it!

Windows Command Line Prompt

Before you proceed with this procedure, make sure that have Python installed. To check your Python-installed version on a Windows machine using the Command Line Prompt, follow these steps:

Step 1: Press Win+R to open the Run dialog.

Step 2: Type cmd and press Enter to open the Windows Command Prompt.

Step 3: In the Command Prompt, type python –version and press Enter.

  1. Command Line Prompt for displaying the Python version

The above prompt will command Python display the installed Python version. If you don’t see a response or encounter an error, ensure that Python is installed and added to your environment variables.

MacOS and Linux Terminal

On Linux and macOS systems, you can use the Terminal to check your Python version.

Follow these instructions:

Step 1: For Linux, press Ctrl+Alt+T to open the Terminal. For macOS, go to Finder, click on Applications, choose Utilities, and then select Terminal.

Step 2: Once the Terminal is open, type python –version and press Enter.

The above prompt will command Python to show your current version of Python installed.

If you have multiple Python versions installed on your computer, you may need to use python3 –version to check the version of Python 3.

Now that you know how to check your version from the command line, you can ensure you’re using the correct version for your projects and manage your environment accordingly.

The above was just one way of checking the Python version running on your computer. You can also check the version from a Python script. The section below demonstrates how to check the version using a Python script.

How to Check Python Version from a Script

In this section, you’ll learn how to check the Python version from within a script using different modules.

We will explore the following:

  1. How to check Python version using the sys Module

  2. How to check Python version using the platform Module

This will help you ensure your script runs on the desired version and adapt its behavior based on the version being used.

How to Check Python Version Using The Sys Module

One way to check the Python version from a script is by using the sys module.

The sys module in Python, which is an integral part of its standard library, gives you the ability to tap into various variables, functions, and objects that are either used or maintained by the interpreter.

By doing so, you can engage with the system and the Python runtime environment, taking advantage of a range of features that prove helpful for tasks related to the system, debugging, or managing the execution of your Python scripts.

The sys module provides the version_info attribute, which contains a tuple with the major, minor, and micro version numbers of the Python interpreter.

The code provided below uses the sys module to check the Python version:

import sys

major, minor, micro = sys.version_info[:3]
print(f"Your Python version is {major}.{minor}.{micro}")

This above code will command Python to import sys print module and use it to retrieve the version of Python.

import sys: This line imports the sys module, which provides access to system-specific parameters and functions.

major, minor, micro = sys.version_info[:3]: The sys.version_info attribute returns a tuple containing the components of the Python version, such as major, minor, and micro version numbers.

Here, we are slicing the tuple with [:3] to get the first three elements, which represent the major, minor, and micro version numbers, and assigning them to the respective variables major, minor, and micro.

print(f”Your Python version is {major}.{minor}.{micro}”): This line uses an f-string (formatted string literal) to display the Python version by substituting the values of major, minor, and micro variables in the string.

The output of import sys print will show the Python version in the format “Your Python version is X.Y.Z”, where X, Y, and Z are the major, micro, and minor version numbers, respectively.

  1. Checking Python version using the sys Module

Keep in mind that at times, you might have multiple versions of Python installed and the out will display the most latest Python version.

In addition, you can use an assert statement to make sure your script runs on a minimum required Python version. For example:

import sys

assert sys.version_info >= (3, 6), "Python 3.6 or higher is required"

The above Python code snippet demonstrates how to import the sys module and use it to check if the Python version is 3.6 or higher. If the requirement is not met, an AssertionError with a custom message is raised.

Restricting Python to use specific Python version using sys module

How to Check The Version of Python Installed Using the Platform Module

Another way to check the Python version is by using the platform module Python command.

The platform module in Python is part of the standard library and provides various functions to retrieve information about the underlying platform, system, and hardware on which the Python interpreter is running.

This can include details about the operating system, processor architecture, and the Python implementation itself.

The import platform print provides the python_version() function, which returns a string with the Python version.

Here’s a sample code demonstrating how to use the platform module:

import platform

version = platform.python_version()
print(f"Your Python version is {version}")

The above Python code snippet demonstrates how to import the platform print module and use it to retrieve the Python version information, then print it in a formatted string.

Checking Python version using the platform Module

You can also use the Python command below which uses tuples to check the Python version:

import platform

version_tuple = platform.python_version_tuple()
print("Your Python version is {}.{}.{}".format(*version_tuple))
Using Tuples with platform module to show Python version

Both the sys and platform modules provide various Python commands to obtain information about the Python environment, and you can choose the one that better fits your specific use case or requirements.

Most often, programmers use IDEs for writing code. You can check the major version in your IDE as well.

In the section below, we will explore how to check Python version in two of the most popular IDEs for Python programming: VSCode and PyCharm.

How to check Python Version in Integrated Development Environments (IDE)

An Integrated Development Environment is a software application that offers a comprehensive set of tools and features to help programmers write, edit, debug, and compile code more efficiently.

Checking the Python versions in an Integrated Development Environment (IDE) is a common task when working on Python projects. Each IDE has its own way of displaying the Python version. In this section, we will discuss the following:

  1. How to check Python version in Visual Studio Code

  2. How to check Python version in PyCharm

How to check Python versions in Visual Studio Code

Visual Studio Code (VSCode) is a popular IDE for Python development. To check the Python version in VSCode, follow these steps:

Step 1: Open Visual Studio Code.

Navigation to open VSCode

Step 2: Open a Python file or create a new one in your project.

Making a new file in Python

Step 3: Click on the Python version displayed in the bottom-left corner of the status bar.

If you don’t see any Python version, make sure that the Python extension is installed.

Click on the Python version to see the current Python version

A dropdown list will appear with the available Python interpreters.

The current active interpreter has a checkmark next to it, and the version number is displayed on the right side.

Current Python version shown in VSCode IDE

How to check Python version in PyCharm

PyCharm is another widely used IDE for Python development. To check the Python version in PyCharm, follow these steps:

Step 1: Open PyCharm.

Step 2: Go to the main toolbar and click on “File” > “Settings” (or “Preferences” on macOS).

Navigating to Settings Panel in PyCharm

Step 3: On the left-side panel, select “Project: Your_Project_Name” > “Python Interpreter”.

Navigating to Python Interpreter in PyCharm

Step 4: On the right-side panel, you will see the Python version installed in your virtual environment listed next to the interpreter executable path.

Current Python version in PyCharm

By following these steps in each respective IDE, you can easily check which version of Python your project is using.

Now that you have knowledge about the Python version you are using, what can you do with this information?

At times, you might receive files from a coworker where the code written is in a newer version of Python, whereas you might be using an older version or vice versa. In such cases, you’ll first need to identify the Python version installed on your system.

What if you’d like to update your Python version to the latest version as you might have different Python versions installed? In the section below, we have listed down a few ways you can use to update your Python version to the latest version.

How to Update a Python Version to The Latest Version

Updating a Python version is as simple as installing Python, however, the procedure for updating varies depending on the operating system you are using.

In this section, we’ll provide an overview of the steps necessary to update Python on Windows, Linus and MacOS.

Updating Python on Windows

Step 1: Uninstall the Python version

Go to Control Panel > Programs and Features.

Navigation to Programs and Features in Control Panel

Find Python in the list of installed programs, click on it, and then click ‘Uninstall.

Find Python in installed programs and uninstall it

Step 2: Download and install the latest Python version

  1. Visit the Python official download page at https://www.python.org/downloads/.

  2. Click on the “Download Python” button

    Download button on Python's website
  3. Run the installer

    Running the Python installer
  4. Follow the installation instructions.

    Configuring Python installation settings
  5. Ensure you tick the “Add Python to PATH” checkbox during installation.

    Configuring Environmental Variables in Python

After clicking the install button, Python will start being installed on your system

New Python version installation in Progress

Updating Python on macOS:

Step 1: Install Homebrew (if you haven’t already)

  1. Open the Terminal app.

  2. Enter the following command, and press Enter:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Step 2: Update Python

In Terminal, enter the following commands, pressing Enter after each:

brew update
brew upgrade python

Updating Python on Linux (Debian-based distributions):

Step 1: Update package lists

  • Open the Terminal app.

  • Enter the following command, and press Enter:

sudo apt-get update

Step 2: Install the latest Python version

Enter the following command, and press Enter:

sudo apt-get install python3.x

Step 3: Set the default Python version (optional)

Enter the following commands, pressing Enter after each:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.x 1
sudo update-alternatives --config python3

Replace “x” with the latest minor version number, and choose the appropriate version when prompted.

By following these steps, you’ll be able to update Python to the latest version on your Windows, macOS, or Linux system.

Let’s Wrap This Up

Throughout this article, we’ve walked you through a comprehensive guide on how to check which Python version you’re using. We’ve also shown you how to check the Python version directly from a script by utilizing the sys and platform modules.

On top of that, we’ve delved into verifying the Python version in two of the most popular IDEs for Python development: Visual Studio Code and PyCharm.

And, we’ve also laid out the steps you need to follow to update your Python version on Windows, macOS, and Linux systems.

By adhering to these instructions, you can keep your Python environment current, take advantage of the latest features and enhancements, and guarantee the success of your projects.

Looking to learn more about Python, check our Python playlist below:

Related Posts