Skip to content

How to Check Your Python Version (Windows, macOS, Linux)

How to Check Your Python Version (Windows, macOS, Linux) Cover Image

In this tutorial, you’ll learn how to check your Python version in Windows, macOS, and Linux. You’ll learn how to check the version of Python using the command line and within a Python script itself. You’ll learn how to get the version number of the interpreter that your scripts will use.

Knowing how to do this is an important skill for any Python developer. For example, it can be an important skill in order to better troubleshoot your code. If your interpreter is set to a different version than you’re expecting (say, Python 2 versus Python 3), being able to identify the version of the interpreter can help troubleshoot your problems.

By the end of this tutorial, you’ll have learned:

  • How to check the Python version of your interpreter in Windows, Mac OS, and Linux
  • How the check the Python version while running your script
  • How to access the major, minor and micro versions of your Python version programmatically

Let’s get started!

How to Check Your Python Version

To check the version that your Python interpreter is running we can use a version command on the python command. Because accessing the command line prompt or terminal varies from system to system, this part of the tutorial is split across the different operating systems available to you.

Over the following sections, you’ll learn how to check your Python version using Windows 10, Windows 7, macOS, and Linux.

How to Check Your Python Version on Windows 10

In Windows 10, we can use the PowerShell to check the version of Python that we are running. In order to access the PowerShell, simply use the following steps:

  1. Press Windows + R
  2. Type powershell
  3. Press OK or hit enter

Once the PowerShell is open you can access the Python version your interpreter is running by writing the commands shown below.

CommandDescriptionSample Output
python --versionShortened information about Python versionPython 3.8.5
python -VShortened information about Python versionPython 3.8.5
python -VVExtended information about the Python versionPython 3.8.5 (default, Sep  4 2020, 02:22:02) 
[Clang 10.0.0 ]
Different commands to check your Python version on Windows 10

How to Check Your Python Version on Windows 7

On Windows 7 and earlier, you can use the command prompt to check your Python version. In order to do this, you can run the following steps:

  1. Press Windows + R
  2. Type cmd
  3. Press OK or hit enter

Check the table below for the different types of commands you can run in order to check your version of Pyhton on Windows 7:

CommandDescriptionSample Output
python --versionShortened information about Python versionPython 3.8.5
python -VShortened information about Python versionPython 3.8.5
python -VVExtended information about the Python versionPython 3.8.5 (default, Sep  4 2020, 02:22:02) 
[Clang 10.0.0 ]
Different commands to check your Python version on Windows 7

In the next section, you’ll learn how to check your Python version on macOS or Linux.

How to Check Your Python Version on macOS / Linux

In macOS and Linux you can use the Terminal application to check your version of Python. On macOS, this can be accomplished by following the steps below:

  1. Open Spotlight by hitting command + space
  2. Type in Terminal and hit enter

Because some versions of macOS come with Python 2 installed by default, it’s likely that the python command may refer to Python 2. Because of this, you may need to run python3 instead, to refer to that version of Python.

The table below breaks down the different commands to run on macOS and Linux to get the version of Python:

CommandDescriptionSample Output
python3 --versionShortened information about Python versionPython 3.8.5
python3 -VShortened information about Python versionPython 3.8.5
python3 -VVExtended information about the Python versionPython 3.8.5 (default, Sep  4 2020, 02:22:02) 
[Clang 10.0.0 ]
python --versionShortened information about Python versionPython 2.7.18
python -VShortened information about Python versionPython 2.7.18
Different commands to check your Python version on macOS and Linux

In the next sections, you’ll learn how to check the version of Python your code is running in your script using the sys and platform libraries.

How to Check Your Python Version in a Script Using sys

The sys library provides a number of different ways in which you can access the version of Python you’re running.

One of the simplest ways is to simply print out the .version attribute. This returns the same information as the -VV command shown above. Let’s take a look:

# Checking the Python Version Using the sys library
import sys
print(sys.version)

# Returns:
# 3.8.5 (default, Sep  4 2020, 02:22:02) 
# [Clang 10.0.0 ]

Because this returns a multi-line string, it’s not immediately accessible. For example, if you wanted to check whether or not a script is running in Python 3.6 or later (to make use of f-strings, for example), you’d have a hard time parsing this.

Instead of using the .version attribute, we can use the .python_version attribute to make this process better. It returns a named tuple that provides all the information in an easy to access manner.

The tuple returned has the following attributes: (major, minor, micro, releaselevel, serial). Let’s see how we can check the major and minor version to see if we can use Python f-strings:

# Checking the Python Version Number Programatically
import sys

if sys.version_info.major == 3 and sys.version_info.minor >= 6:
    print(f'You are running Python {sys.version_info.major}.{sys.version_info.minor}!')
else:
    print('Sadly, no f-strings!')

# Returns:
# You are running Python 3.8!

In the code above we checked if our system was capable of running Python f-strings. In the next section, you’ll learn how to use the platform library to check your Python version.

How to Check Your Python Version in a Script Using platform

The Python platform library provides a number of helpful methods that are very similar to the ones provided by the sys library. For example, the python_version() function can be used to return a string that contains the Python version.

Let’s see how we can import the library and return a string containing our current Python version:

# Checking Your Python Version with platform
import platform
print(platform.python_version())

# Returns: 3.8.5

Similar to the sys library, the platform library provides a way to extract a tuple. Unlike the sys library, the tuple that’s returned is not a named tuple and each item in the tuple is a string, not an integer. Let’s take a look at an example:

# Checking Your Python Version with platform
import platform
print(platform.python_version_tuple())

# Returns: ('3', '8', '5')

Conclusion

In this tutorial, you learned how to check your version of Python on Windows, macOS and Linux. You also learned how to check the version your code is running in your script using both the sys and platform library. Being able to check the version of Python you’re using allows you to better troubleshoot your code and identify any problems that are caused by versions.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *