How to check which version of Python is running in a script?

Sometimes, we want to check which version of Python is running in a script.

In this article, we’ll look at how to check which version of Python is running in a script.

How to check which version of Python is running in a script?

To check which version of Python is running in a script, we can use the sys.version and sys.version_info properties.

For instance, we write:

import sys

print(sys.version)
print(sys.version_info)

sys.version provides basic version info.

And it returns data like:

3.8.12 (default, Sep 10 2021, 00:16:05) 
[GCC 7.5.0]

sys.version_info provides more detailed version info.

It returns data like:

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

Conclusion

To check which version of Python is running in a script, we can use the sys.version and sys.version_info properties.