Method 1: sys.version
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)]
Method 2: sys.version_info
If you need an easy-to-process output for the major, minor, and micro versions, use the
sys.version_info attribute. For example, Python version 3.9.5 has major version 3, minor version 9, and micro version 5. You can access the major version with
sys.version_info, the minor version with
sys.version_info, and the micro version with
import sys print(sys.version_info) # sys.version_info(major=3, minor=9, micro=5, releaselevel='final', serial=0) print(sys.version_info) # Major: 3 print(sys.version_info) # Minor: 9 print(sys.version_info) # Micro: 5
Method 3: platform.python_version()
platform.python_version() function returns a string representation of the form
'major:minor:micro'. For example, you can easily split it on the dots to obtain the respective major, minor, or micro version info returned by this function.
import platform print(platform.python_version()) # 3.9.5
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.