How to Check Current Python Version
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 TypeScript
- Getting Started with Svelte
- Getting Started with Express
- Create an RSS Reader in Node
- Getting Started with Electron
- How to Serve Static Files with Nginx and Docker
- How to Set Up Cron Jobs in Linux
- How to build a Discord bot using TypeScript
- How to deploy a Node app using Docker
- Getting Started with Sass
- Using Puppeteer and Jest for End-to-End Testing
- Setting Up Stylus CSS Preprocessor