How to check Python’s version from command line and in script

This post will introduce you useful ways to check what version of Python is running.

Ways to check Python’s version

Python developers often find themselves in a situation where they are unaware of the most recent security updates and vulnerabilities. This is because many organizations choose to use Python as their development language, but have not yet updated it since its release date or do not know how to update software.

You need to know your current Python version to see if it has security holes and to download correct external library for running version.

And upgrading has many benefits:

  • Your app will be protected against new vulnerabilities that others may exploit.
  • Updating on time ensures compliance with licensing agreements.
  • Your code will run more efficiently when utilizing newer versions which provide better performance.
  • Python’s version can be check on the command line with --version-V-VV
  • Python’s version can be check in script with sysplatform

Check version on the command line

Commands below can be used if both Python 2 and 3 are installed on your system and the default running one is Python 2. python --version prints out the current running version.

python --version
>> Python 2.7.17

python -V
>> Python 2.7.17

python3 --version
>> Python 3.6.9

python3 -V
>> Python 3.6.9

Check version in script

 sys and platform module 2 default libraries which can be used to get the version of Python that is actually running.

They are used in the situation where you don’t know which Python are powering your app. So you need to add a version check in the app’s script to make sure.

import sys
import platform

print(sys.version)
>> 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]

print(sys.version_info)
>> sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)

print(platform.python_version())
>> 3.6.9
print(platform.python_version_tuple())
>> ('3', '6', '9')

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close