How to Check Python Version

I was troubleshooting a compatibility issue in a project where some features weren’t working as expected. The culprit turned out to be version-related – I was using Python 3.7 when the codebase required Python 3.9 or newer.

This is a common scenario that many developers face. Knowing which Python version you’re running is crucial for compatibility, debugging, and ensuring your code works as expected.

In this article, I’ll share five simple methods to check Python version. These approaches work across Windows, macOS, and Linux, so you’ll be covered regardless of your operating system.

Method 1 – Use Python’s Command Line

The easiest way to check your Python version is through the command line interface. This method works universally across all operating systems.

Here are the steps to check your Python version using the command line:

  1. Open your terminal or command prompt
  2. Type one of the following commands:
python --version

Or

python -V

You can see the output in the screenshot below.

How to Check Python Version

If you have multiple Python versions installed, you might need to specify which Python installation you want to check:

python3 --version

When you run this command, you’ll see output similar to this:

Python 3.11.7

You can see the output in the screenshot below.

Check Python Version

This tells you the major version (3), minor version (9), and micro version (7) of your Python installation.

Method 2 – Use Python’s sys Module

Another reliable way to check your Python version is by using the built-in sys module, which provides access to some variables and functions maintained by the interpreter.

Here’s how to use the sys module:

  1. Open a Python interactive shell by typing python in your terminal
  2. Type the following code:
import sys
print(sys.version)

This will display more detailed information about your Python version, including the build date and compiler used:

3.13.7 (default, Sep 16 2021, 13:09:58) 
[GCC 7.5.0]

You can see the output in the screenshot below.

Check Version in Python

If you only need the version numbers without the extra information, you can use:

import sys
print(sys.version_info)

This will output something like:

sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)

Method 3 – Use Python’s platform Module

The platform module provides an alternative way to retrieve the Python version, and it’s particularly useful when you need a clean version string:

import platform
print(platform.python_version())

This outputs just the version number:

3.9.7

You can see the output in the screenshot below.

Python Version

For even more detailed information, you can use:

import platform
print(platform.python_implementation())
print(platform.python_version_tuple())

The output will look something like this:

CPython
('3', '9', '7')

This tells you both the Python implementation (CPython, PyPy, etc.) and provides the version as a tuple that you can easily parse.

Method 4 – Check Version in Python Scripts

When writing automation scripts or setup files, you might need to verify the Python version programmatically. Here’s a simple function you can use:

def check_python_version():
    import sys
    required_version = (3, 9)
    current_version = sys.version_info[:2]

    if current_version >= required_version:
        print(f"Python version check passed. Current version: {sys.version}")
        return True
    else:
        print(f"Python version check failed. Required: {required_version}, Current: {current_version}")
        return False

# Example usage
if check_python_version():
    print("Continuing with execution...")
else:
    print("Exiting due to version incompatibility.")
    exit(1)

You can see the output in the screenshot below.

version python

This function checks if the current Python version meets a minimum requirement (in this case, Python 3.9). It’s particularly useful for ensuring compatibility at the beginning of scripts that require specific Python features.

Method 5 – Use Virtual Environment Information

If you’re working with virtual environments (which I highly recommend), there’s another way to check the Python version used in that environment:

  1. Activate your virtual environment
  2. Run the following command:
pip list

Look for the line that says “pip” and note its version. Then run:

pip --version

This will show output like:

pip 21.2.4 from /path/to/your/venv/lib/python3.9/site-packages/pip (python 3.9)

The Python version used in your virtual environment is included at the end of this line.

Which Python Version Should You Use?

While checking your Python version is important, choosing the right version for your project is equally crucial. Here’s my general advice:

  • For new projects: Use the latest stable version (Python 3.11 as of writing). It offers the best performance and newest features.
  • For professional work: Match the version used by your team or specified in the project requirements.
  • For learning: Python 3.8 or newer is recommended to access modern Python features.

Remember that Python 2 reached end-of-life in 2020, so you should avoid it for new projects unless you’re maintaining legacy code.

I personally recommend staying within one or two versions of the latest release to benefit from security updates and new features while maintaining good compatibility with most libraries.

Python’s evolution continues to make the language more powerful and developer-friendly, so keeping your skills updated with newer versions is a worthwhile investment for any Python developer.

By now, you should be able to confidently check which Python version you’re using through multiple methods. This fundamental skill will help you troubleshoot compatibility issues and ensure your development environment meets the requirements of your projects.

Other Python articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.