Table of Contents
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:
BASHpython -V
You should get output like this:
BASHPython 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:
PYTHONimport sys
print(sys.version_info)
You should get output like this:
BASHsys.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:
PYTHONimport 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!
Getting Started with Solid
Getting Started with Svelte
Best Visual Studio Code Extensions for 2022
How to deploy a Deno app using Docker
Getting Started with Deno
How to deploy an Express app using Docker
Build a Real-Time Chat App with Node, Express, and Socket.io
Getting User Location using JavaScript's Geolocation API
Getting Started with Moment.js
Learn how to build a Slack Bot using Node.js
Using Push.js to Display Web Browser Notifications
Setting Up a Local Web Server using Node.js
