February 8, 2025

How to Check Python Version

Knowing the version of Python you are using is important for compatibility and debugging purposes. Different versions of Python might have different features, libraries, and syntax. Here are several methods to check the version of Python installed on your system:

1. Using the Command Line

1.1 Checking Python Version in the Terminal or Command Prompt

To check the Python version from the command line, you can use the following commands:

# For Python 3.x
python3 --version

# Alternatively
python3 -V

In some systems, Python 3 may be aliased to python instead of python3, so you can also use:

# For Python 3.x
python --version

# Alternatively
python -V

These commands will display the version of Python 3.x installed on your system. For example, you might see:

Python 3.8.10

2. Using Python Script

2.1 Checking Python Version from within a Python Script

You can also check the Python version programmatically by using the sys module in a Python script:

import sys

# Print the version of Python
print(f"Python version: {sys.version}")

This will output something like:

Python version: 3.8.10 (default, May  3 2021, 08:53:12) 
[GCC 8.4.0]

The sys.version attribute provides a string containing the Python version number along with additional information about the build.

3. Using an Integrated Development Environment (IDE)

3.1 Checking Python Version in Popular IDEs

Most IDEs provide a way to view the Python version used by the interpreter configured for the project:

  • PyCharm: Go to File > Settings > Project > Project Interpreter to see the Python version.
  • Visual Studio Code: Check the Python interpreter path in the bottom left corner or use the command palette (Ctrl+Shift+P) and type Python: Select Interpreter.
  • Jupyter Notebook: Run !python --version in a notebook cell to check the Python version.

4. Summary

Checking the Python version can be done through various methods including command line commands, Python scripts, and IDE interfaces. Understanding which version of Python you are using helps ensure compatibility with libraries and code, and helps in debugging and development processes.