Check Python Version: A Simple Illustrated Guide

The Best Way to Check Python Version (3 Easy Steps):

  1. Open your command line (CMD), Powershell (Win), or terminal (Linux/Mac)
  2. Type python -V and hit Enter
  3. To check your Python 3 version, type python3 -V

To check your Python version, run python ‐V in your command line (Windows), shell (Mac), or terminal (Linux/Ubuntu). To check your Python version in your script, run import sys to get the module and use sys.version to find detailed version information in your code.

In the following screenshot, you can see how I checked the Python version in my Windows command line (similar for PowerShell):

Let’s get a quick overview of the different ways to check your Python version in all operating systems and environments. This may be all you need to know:

CommandsWhere?What?Example Output
python ‐‐version or
python -V or
python -VV
TerminalMac/Linux/WinPython 3.7.2
import sys
sys.version
Python ScriptInformation string'3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018,
23:09:28) [MSC v.1916 64 bit (AMD64)]'
sys.version_infoPython scriptVersion info tuplesys.version_info(major=3, minor=7, micro=2, releaselevel='final', serial=0)
import platform
platform.python_version()
Python scriptShort string info'3.7.2'
platform.python_version_tuple()Python scriptShort tuple info('3', '7', '2')

I’ve showcased all seven of these ways in the following graphic:

7 Ways to Check Python Version

In the following video, I’ll show you how to check your Python version in all operating systems (Windows, macOS, Linux, Ubuntu) and programming frameworks (e.g., Jupyter):

Get Python Version in All Environments (CMD, macOS, Linux, Windows, Ubuntu, Python Script)

This general method works across all major operating systems (Windows, Linux, and macOS).

OS & EnvironmentMethod
Win10, Win7Open command line and run python -V or python ‐‐version
MacOS, Linux, UbuntuOpen terminal and run python -V or python ‐‐version
Python Shell, Juypter NotebookInteractive mode:
>>> import sys
>>> sys.version
Python Editor, Juypter NotebookNormal mode:
import sys
print(sys.version)

The table shows you how different environments call for different commands to check your Python version.

You may have the following question:

How to open your command line or terminal?

  • Windows:  Hit shortcut Win+R, type powershell, hit OK.
  • MacOS:  Hit shortcut Cmd+Space, type terminal, hit OK.
  • Linux:  Hit shortcut Ctrl+Alt+T.

The Python version output consists of three numbers major:minor:micro. For example, version 3.7.2 means that

  • the major version is 3,
  • the minor version is 7, and
  • the micro version is 2.

🤚 ATTENTION: Different major versions are NOT fully compatible. Different minor versions are compatible.

For example, you can execute code written in Python 3.6.4 in Python 3.7.2 because they are the same major version — Python 3. But you cannot execute code written in Python 2.7.4 in Python 3.7.2 because they are different major versions.

💡 Note: New minor versions can add changes to the language. For example, Python 3.8 introduced the reversed() function with dictionaries. You cannot use the reversed() function in older versions of Python. But the vast majority of the language is the same.

Let’s dive into the exact steps to check your Python version in any environment.

Check Which Python Version in Script (Exact Steps)

Sometimes, you want to check Python’s version in your Python program.

To achieve this, simply import the sys module and print the sys.version attribute to your Python shell:

>>> import sys
>>> print(sys.version)
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]

Check Python Version Jupyter (Exact Steps)

Three steps to check the Python version in a Jupyter Notebook.

  1. Open the Jupyter Notebook: type jupyter notebook in your terminal/console.
  2. Write the following Python code snippet in a code cell:
from platform import python_version
print(python_version())

3. Execute the script.

As an alternative, you can also use the following Python code snippet to check your Python version in a Jupyter notebook:

>>> import sys
>>> sys.version

Here is a screenshot on my computer:

Check Python Version Windows 10 (Exact Steps)

Three steps to check the Python version on your Win 10 operating system:

  1. Open the Powershell application: Press the Windows key to open the start screen. In the search box, type “powershell”. Press enter.
  2. Execute command: type python ‐‐version and press enter.
  3. The Python version appears in the next line below your command.

Check Python Version Windows 7 (Exact Steps)

Three steps to check the Python version on your Win 7 operating system.

  1. Open the command prompt application: Press the Windows key to open the start screen. In the search box type “command”. Click on the command prompt application.
  2. Execute command: type python ‐‐version and press Enter.
  3. The Python version appears in the next line right below your command.

Check Python Version Mac (Exact Steps)

Four steps to check the Python version on your Mac operating system.

  1. Press CMD + Space to open Spotlight.
  2. Type “terminal” and press enter.
  3. Execute command: type python ‐‐version or python -V and press Enter.
  4. The Python version appears in the next line below your command.

Check Python Version Linux (Exact Steps)

Three steps to check the Python version on your Linux operating system.

  1. Open the terminal application (for example, bash).
  2. Execute command: type in python ‐‐version or python -V and press Enter.
  3. The Python version appears in the next line below your command.

Check Python Version Ubuntu (Exact Steps)

Four steps to check the Python version on your Ubuntu operating system.

  1. Open Dash: click the upper left symbol.
  2. Open terminal: type “terminal“, click on the terminal app.
  3. Execute command: type python ‐‐version or python -V and press Enter.
  4. The Python version appears in the next line right below your command.

Check Your Python Version in Anaconda (Exact Steps)

You can choose from different alternatives.

  • To check your Anaconda version, run conda -V or conda --version in your anaconda prompt. You can open the anaconda prompt by searching for “anaconda prompt” in your OS’s search field.
  • An alternative to get your Anaconda version is to run conda list anaconda.
  • The shorter command conda list lists the name, version, and build details of your installed packages.
  • To learn about your environment details, run conda info with the optional flag ‐‐envs to see all your environments.
  • To check your Python version, run python -V or python ‐‐version in your terminal.

👉 Recommended: Please find more detailed information about setting up your environment here. You can download an informative Anaconda cheat sheet here.

Check Your Python Version in Spyder (Exact Steps)

In your Spyder code editor, it’s even simpler to check your Python version.

Just run any script and the version information will appear in the first line before the output of your script.

How to Upgrade to a Newer Version?

If you are not using a virtual environment, go to python.org/downloads to download and install whatever version you need. It’s the easiest way to upgrade Python.

But now you’ll run into the following problem: how do I run a specific Python version? Check out this StackOverflow answer to learn the exact steps.

Or you can make your life easier by using virtual environments.

These let you have multiple versions of Python installed on your system. Plus, you can switch between them instantaneously.

One option is to use the built-in module venv. If you’re a Data Scientist, the industry standard is Anaconda.

Installing and upgrading different Python versions is easy when you use virtual environments. For a full tutorial of virtual environments, read over our introductory Finxter blog article.

How to Check if Python 3 is Installed?

If you’ve installed multiple installations of Python, running python ‐‐version may give you only the version of Python 2.

To check which version of Python 3 is installed on your computer, simply run the command python3 ‐‐version instead of python --version.

How to Check Python Version – Detailed

Not only does Python have major, minor and micro versions. Each of those versions has further versions, namely the release level and serial.

These are displayed when you run

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0)

In the above code, I am running Python 3.8.0.

Most of the time, you will only care about the major, minor and micro releases. Release level and serial are usually for the core Python developer team to work on changes to the language.

The possible release levels are ‘alpha’, ‘beta’, ‘candidate’, or ‘final’.

  • Alpha contains the first updates made to the language.
  • Beta means the language can be tested with some users but still won’t work perfectly. This is where the phrase ‘beta testers’ comes from.
  • Candidate has only a few small bugs left to fix.
  • Final is the last version and the one released to the general public.

You can download these release levels if you want to try out new features before anyone else.

However, if you just want a version of Python that works, you should choose ‘final’. When you download any version of Python, it will be a ‘final’ release unless stated otherwise.

Serial is for the smallest changes. The Python-Dev team increments it as they make changes to the alpha, beta and candidate versions.

All final versions have serial=0. They add future changes to the next major/minor/micro releases.

How to Make Sure My Script Runs a Specific Python Version?

Let’s say you’ve just installed Python 3.8.

Your script, my_file.py, uses a brand new feature: reversed() when iterating over a dictionary.

For other people to run this script, they must also run Python 3.8. So you should put a check at the start to let other users know this.

We do this by adding an assert statement at the top of my_file.py

# my_file.py
import sys
assert sys.version_info >= (3, 8)

my_dict = dict(a=1, b=2, c=3)
for key in reversed(my_dict):
    print(key)

The assert statement raises an AssertionError if the statement is False. If the statement is True, the script continues to run.

For example, if I am running Python 3.7 and execute my_file.py from the terminal, this happens:

# Running Python 3.7
$ python my_file.py
Traceback (most recent call last):
  File "my_file.py", line 10, in <module>
    assert sys.version_info >= (3,8)
AssertionError

But if I am running Python 3.8, the assert statement does not raise an error, and it executes the rest of the script.

# Running Python 3.8
$ python my_file.py
c
b
a

Note: I have used the Anaconda virtual environment to install and quickly switch between multiple Python versions.

Summary

Check the Python version by typing python ‐‐version in your operating system shell or command line.

To get more detailed information about the environment your Python program runs in, try import sys; sys.version in your Python shell (interactive or normal mode).

Programmer Humor

Q: What is the object-oriented way to become wealthy?
💰

A: Inheritance.