×

How To Find Python Version on Linux/Ubuntu? – LinuxWays

How To Find Python Version on Linux/Ubuntu? – LinuxWays


Python is a popular programming language mainly used for Task Automation, Developing Data-Centric Software Systems, and Data Visualization. Python is used as a programming language for many software applications, so it is crucial to know which version of Python we are using. Knowing the Python Version helps us in using compatible tools and applications with the system.

This article explains different methods to check the Python Version installed on Ubuntu.

How To Find Python Version on Linux/Ubuntu?

Checking the Python version on Ubuntu helps us avoid any compatibility issues while installing any new software on a Linux/Ubuntu machine. To find the installed Python Version on Ubuntu, use the Methods mentioned below:

  • Using the Python3 Command
  • Using the “sys module” script
  • Using “platform” Command

Method 1: Use the Python3 Command to Find Python Version on Linux/Ubuntu

The simplest method is to use the “python3” command to check the Version details. Use the Python3 command along with the “-V” flag to display the version only:

The “-V” flag shows only the version of installed Python on Ubuntu:

Method 2: Use the “sys module” Script to Find Python Version on Linux/Ubuntu

Python Version can also be checked using a Script. To use Script for checking the Python Version, create a file with the extension “.sh”:

The touch command will create the file:

Open the “python.sh” file in a Text Editor like Nano:

In the “python.sh” file, add the code below:

import sys

print(“Python Version: “)
print(sys.version)

The “sys” is a Library in Python used to provide information about the Python Interpreter:

Once the above code is added, save the file using the “ctrl+o” shortcut key and then close it using the “ctrl+x” shortcut key. This will make the script file:

Make the Script File executable now by using the “chmod +x” command. The “ +x” flag makes the file runnable:

Once the Script File is executable, use the command below to execute it:

This will execute the file by running the code inside the script file and will print the Python Version as follows:

Method 3: Use the “platform” Command to Find the Python Version on Linux/Ubuntu

The “platform” command can also be used to show the current Python Version on Ubuntu. Use the command below with the platform command to print the current Python Version:

python -c “import platform; print(platform.python_version())”

The “-c” flag is used to specify the configuration file or retrieve information from a config file:

This sums up how to find the Python version on Linux/Ubuntu.

Conclusion

Knowing the Python Version helps developers install compatible software and tools for efficient and stable running of Ubuntu. Python Version on Ubuntu can be checked using either the “python3”, using the “sys module” in Script File, or using the “platform” command. This article has explained in detail the use of all methods to check the Python Version in Ubuntu.



Source link