How to Check Current Python Version

Updated onbyAlan Morel
How to Check Current Python Version

Knowing what version of Python you are using is important, especially because of how much changed between Python 2 and 3.

Thankfully, there are two main ways you can check your version of python.

In this post, we'll learn about the best ways to check your version of Python.

Using the Command Line

Before you even write any code, you can check your version of Python using the command line.

Python includes a built-in command line utility called python -V that will tell you what version of Python you are using.

Run it like this:

BASH
python -V

You should get output like this:

BASH
Python 3.10.4

At runtime

The other way you can check your version of Python is at runtime.

Python has a built-in function called sys.version_info that will return to you the version of Python you are using.

Simply import the sys module and call the version_info function:

PYTHON
import sys print(sys.version_info)

You should get output like this:

BASH
sys.version_info(major=3, minor=10, micro=4, releaselevel='final', serial=0)

This is great because since you can use it at runtime, you can add checks in your code to behave differently depending on the version of Python you are using.

Here's how you can add an upgrade message if the user is not using the Python version you require:

PYTHON
import sys if sys.version_info < (3, 10): print("You need to upgrade to Python 3.10 or greater to use this program.") sys.exit(1)

Conclusion

In this post, we learned how to check your version of Python using the command line and at runtime.

Checking at runtime is useful because it allows your program to tell the user to upgrade if they are using an older version of Python.

Hopefully, this has been useful for you. Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.