How do I check which version of Python is running my script?

There are a few ways to check which version of Python is running your script.

One way is to use the sys module, which provides information about the Python runtime. You can use the sys.version or sys.version_info attributes to check the version of Python. Here's an example:

import sys

print(sys.version)
# or
print(sys.version_info)

sys.version returns a string that contains information about the version of Python, including the version number, build date, and compiler.

sys.version_info returns a named tuple that contains information about the version of Python, including the major, minor, and micro version numbers. For example:

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

Watch a course Python - The Practical Guide

Another way to check the version of Python is by running the command python -V in the command line. This will print the version of Python that's currently installed on your system.

If you are inside a running python script or in the interpreter, you can use the built-in platform module:

import platform

print(platform.python_version())

This will print only the version number as a string, for example "3.8.2"

All the above methods would give the version of python which is running the script.