How to Check Python Version in Linux?

Learn via video courses
Topics Covered

The command-line interface in Linux may be used to verify the Python version. Either type "python --version" or "python -V" into the terminal window on your Linux computer. Your system's installed version of Python will be shown when you do this. The Python version you want to verify must be specified if you have multiple Python versions installed, such as Python 2 and Python 3. The commands "python2 --version" and "python3 -V" may be used to determine the version of Python 2, while "python3 --version" and "python3 -V" can be used to determine the version of Python 3.

The output of the command, in the form "Python X.Y.Z," shows the installed Python version. The major, minor, and micro versions are denoted by the letters X, Y, and Z, respectively. When you get "Python 3.8.10" as the output, for instance, it signifies that Python 3 is installed and that the current version is 3.8.10.

You may verify the Python version that is installed on your machine under Linux in several different methods. This knowledge is essential for guaranteeing compatibility and executing the appropriate Python scripts or apps. In this article, we'll look at two approaches that are frequently used to determine the Python version under Linux: using the "python -V" command and using the "sys" module inside a Python script.

Using Python -V Command

One straightforward way to check the Python version is by using the command line interface. Follow these steps:

  • Open a terminal:
    Launch the terminal application on your Linux system. This can usually be found in the applications menu or by searching for "Terminal".
  • Enter the command:
    In the terminal, type "python -V" (or "python --version") and press Enter. Make sure you use a lowercase "v" and include a space between "python" and "-V".
  • View the output:
    After executing the command, the terminal will display the installed Python version. The output will be in the format "Python X.Y.Z," where X represents the major version, Y denotes the minor version, and Z indicates the micro version. For example, "Python 3.8.10" means you have Python 3 installed, with version 3.8.10.

If you have both Python 2 and Python 3 installed, you may need to use specific commands to check each version separately. For Python 2, use "python2 -V" or "python2 --version", and for Python 3, use "python3 -V" or "python3 --version".

Using sys Module

The "sys" module in Python provides access to system-specific parameters and functions, including information about the Python interpreter. By utilizing the "sys" module, you can programmatically retrieve the Python version in Linux. Here's how you can do it:

  • Open a text editor:
    Launch a text editor on your Linux system, such as Vim, Nano, or Gedit.

  • Create a new Python script:
    In the text editor, create a new file and save it with a ".py" extension. For example, you can name it "python_version.py".

  • Import the "sys" module:
    Within the Python script, import the "sys" module by adding the following line at the beginning:

  • Retrieve the Python version:
    After importing the "sys" module, you can access the Python version information using the "sys.version" attribute. Add the following lines to your script:

    The "sys.version" attribute returns a string that contains the Python version along with additional information like the build number and compiler details.

  • Save and exit:
    Save the Python script and exit the text editor.

  • Run the script:
    Open a terminal, navigate to the directory where you saved the Python script, and execute the following command:

    The Python version will be shown in the terminal after the script has been executed in the format specified by the "sys.version" parameter.

You may get the Python version programmatically by using the "sys" module, which is helpful in a variety of circumstances. For instance, you may use this knowledge to guarantee that your code is compatible with particular Python features or packages. Additionally, it assists in resolving and debugging any problems that could develop as a result of version-specific differences.

You may integrate Python version retrieval into bigger programs or scripts, where you might need to carry out additional activities depending on the particular Python version being used, by utilizing the "sys" module.

Using Platform Module

The "platform" module in Python provides a cross-platform way to access various system-related information, including the Python version. By utilizing the "platform" module, you can retrieve the Python version in Linux. Here's how you can do it:

  • Open a text editor:
    Launch a text editor on your Linux system, such as Vim, Nano, or Gedit.

  • Create a new Python script:
    In the text editor, create a new file and save it with a ".py" extension. For example, you can name it "python_version.py".

  • Import the "platform" module:
    Within the Python script, import the "platform" module by adding the following line at the beginning:

  • Retrieve the Python version:
    After importing the "platform" module, you can use the "platform.python_version()" function to get the Python version. Add the following lines to your script:

    The "platform.python_version()" function returns a string containing the Python version in a concise format, such as "3.8.10".

  • Save and exit:
    Save the Python script and exit the text editor.

  • Run the script:
    Open a terminal, navigate to the directory where you saved the Python script, and execute the following command.

    The Python version obtained using the "platform" module will be displayed in the terminal once the script has been executed.

    The "platform" module offers other features and characteristics such as the operating system, machine architecture, and processor type that might provide more specific system information. Functions like "platform.system()", "platform.machine()", and "platform.processor()" can be used to access these.

You may acquire the Python version without regard to the platform by using the "platform" module. It delivers consistent outcomes in Linux, Windows, and macOS, among other operating systems. This is very helpful when creating programs that must manage many environments.

The "platform" module might also be helpful if you want to take particular actions based on the underlying platform or acquire extra system-related data.

Learn More

  • Linux Operating System (LINK)
  • Basic Linux Commands with Examples (LINK)
  • Advanced Linux Commands (LINK)
  • Understanding the Linux File System and Directory Structure (LINK)
  • Linux Directory Structure (LINK)

Conclusion

  • The "python -V" command in the terminal may be used to determine the Python version running on Linux.
  • Alternatively, you may do the same thing by using the "python --version" command.
  • This technique offers a quick and simple means of showing the installed Python version.
  • To verify particular versions of Python, you might need to provide "python2 -V" or "python3 -V" if you have several installations of the language.
  • Another method is to acquire the Python version programmatically by using the "sys" module in a Python script.
  • The Python version may be printed within the script by importing the "sys" module and using the "sys.version" property.
  • The versatility of this approach enables you to integrate version retrieval into more complex programs or scripts.
  • Another choice for retrieving the Python version cross-platform is the "platform" module.
  • You may find the Python version by importing the "platform" module and calling the "platform.python_version()" method.
  • Using this strategy yields consistent outcomes on Linux, Windows, and macOS, among other operating systems.
  • To assure compatibility with certain features, libraries, and scripts, it is essential to know the Python version.
  • It makes Python code execution more streamlined and aids in avoiding version-related problems.
  • The main, minor, and micro versions of Python are often shown in the format "Python X.Y.Z" on displays.
  • For debugging and troubleshooting, knowing the version of Python is crucial.