How do you find the python version you are using?

Last update on August 19 2022 21:51:43 (UTC/GMT +8 hours)

Python Basic: Exercise-2 with Solution

Write a Python program to get the Python version you are using.

A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. This string is displayed when the interactive interpreter is started.

Version info:

A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0). The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on.

Note : 'sys' module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.

Sample Solution:

Python Code:

import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)

Sample Output:

Python version                                                                                                
3.5.2 (default, Sep 10 2016, 08:21:44)                                                                        
[GCC 5.4.0 20160609]                                                                                          
Version info.                                                                                                 
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)   

Check the Python version on command line:

Python Code:

[email protected]:~$ python --version
Python 2.7.17
[email protected]:~$ python -V
Python 2.7.17
[email protected]:~$ python3 --version
Python 3.6.9
[email protected]:~$ python3 -V
Python 3.6.9 

Using platform module:

Python Code:

import platform
print(platform.python_version())

Python version as tuple (major, minor, patchlevel) of strings:

Python Code:

import platform
print(platform.python_version_tuple())
print(type(platform.python_version_tuple()))

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print the following string in a specific format (see the output).
Next: Write a Python program to display the current date and time.

Introduction

Which version of Python do I have installed?

Python is a popular programming language. Like many other programming languages, there can be several different versions organized by release date. Certain applications may require a specific version of Python.

In this tutorial, learn how to check the Python version on Windows, Linux, or macOS systems.

How do you find the python version you are using?

Prerequisites  

Access to a command-line/terminal window:

  • Linux:  Ctrl-Alt-T, Ctrl-Alt-F2
  • Windows:  Win+R > type powershell > Enter/OK
  • MacOS:  Finder > Applications > Utilities > Terminal

There are different versions of Python, but the two most popular ones are Python 2.7.x and Python 3.7.x. The x stands for the revision level and could change as new releases come out.

When looking at the version number, there are usually three digits to read:

  1. the major version
  2. the minor version
  3. the micro version

While major releases are not fully compatible, minor releases generally are. Version 3.6.1 should be compatible with 3.7.1 for example. The final digit signifies the latest patches and updates.

Python 2.7 and 3.7 are different applications. Software that’s written in one version often will not work correctly in another version. When using Python, it is essential to know which version an application requires, and which version you have.

Python 2 will stop publishing security updates and patches after 2020. They extended the deadline because of the large number of developers using Python 2.7. Python 3 includes a 2 to 3 utility that helps translate Python 2 code into Python 3.

How to Check Python Version in Linux

Most modern Linux distributions come with Python pre-installed.

To check the version installed, open a terminal window and entering the following:

python --version

How do you find the python version you are using?

How to Check Python Version in Windows

Most out-of-the-box Windows installations do not come with Python pre-installed. However, it is always a good idea to check.

Open Windows Powershell, and enter the following:

python --version

If you have Python installed, it will report the version number.

How do you find the python version you are using?

Alternately, use the Windows Search function to see which version of Python you have:

Press the Windows key to start a search, then type Python. The system will return any results that match. Most likely a match will show something similar to:

Python 3.7 (32-bit)

app

Or,

Python 2.7 (32-bit)

app

This defines which major and minor revision (3.x or 2.x) you are using.

How to Check Python Version in MacOS

If using a MacOS, check the Python version by entering the following command in the terminal:

python -version

The system will report the version.

How do you find the python version you are using?

Note: In some cases, this will return a screen full of information. If that happens, just scan through the file locations for the word python with a number after it. That number is the version.

Checking a System with Multiple Versions of Python

Python2 and Python3 are different programs. Many programs upgrade from the older version to the newer one. However, Python 2.7.x installations can be run separately from the Python 3.7.x version on the same system.

Python 3 is not entirely backward compatible.

To check for Python 2.7.x:

python --version

To check the version of Python 3 software:

python3 --version

Most systems differentiate Python 2 as python and Python 3 as python3. If you do not have Python 2, your system may use the python command in place of python3.

Note: Python does not have a built-in upgrade system. You’ll need to download the latest version and install it.

How to Check Python Version in Script

When writing an application, it is helpful to have the software check the version of Python before it runs to prevent crashes and incompatibilities.

Use the following code snippet to check for the correct version of Python:

import sys
if not sys.version_info.major == 3 and sys.version_info.minor >= 6:

    print("Python 3.6 or higher is required.")

    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))

    sys.exit(1)

When this script runs, it will test to see if Python 3.6 is installed on the system. If not, it will send a notification and displays the current Python version.

Conclusion

You should now have a solid understanding of how to check for the version of Python installed in several different operating systems. Python is a powerful programming language, thus it’s important to understand its different versions.

If you want to learn how to upgrade Python to a newer version on Wondows, macOs, and Linux, check our article how to upgrade Python to 3.9.

How do I know my Python version?

Method 1..
Open cmd/terminal/windows powershell..
Write 'python' and press enter key to move into Python interpreter..
Write the same command given in the input box below, and in the result, the user will get the current interpreter version..

How do I check Python version on Windows?

Show activity on this post..
Open Command Prompt > Type Python Or py > Hit Enter If Python Is Installed it will show the version Details Otherwise It will Open Microsoft Store To Download From Microsoft Store..
Just go in cmd and type where python if it installed it will open a prompt ..