Saturday, October 19, 2024

How to Check Python Version on Your Linux Server

Determining the correct Python version is crucial for ensuring compatibility and optimal performance, whether you are running a program designed for a specific version or embarking on a new project. This guide will walk you through three methods for checking the Python version in Ubuntu and other Linux distributions directly from the command line.

Method 1: Checking Python 2 Version via Python Command

If your Linux system runs the older Python 2 version (now deprecated), you can use the "python" command with the "-V" or "--version" option to retrieve version information.

      $ python -V
    

      $ python --version
    

This method is specific to Python 2. Python 3 utilizes a different command, detailed in the next section.

Method 2: Checking Python 3 Version via Python Command

To check the Python 3 version, execute the "python3" command with the "-V" or "--version" option.

      $ python3 -V
    

      $ python3 --version
    

This method is universally applicable across Linux distributions, including Windows and macOS.

Method 3: Checking Python Version via Python Interpreter

While the preceding methods identify the installed Python versions on your system, you can also check the version within the Python interpreter (also known as the Python console) or within a Python script.

The "sys" and "platform" modules provide valuable information regarding the installed Python version. The "sys" module offers detailed information, including the build number and date, whereas the "platform" module provides only the version.

You can choose either module based on your requirements. Simply open your Python console or script and execute one of the following Python snippets:

Checking Python Version Using Sys Module

import sys
print(sys.version)
    

Checking Python Version Using Platform Module

import platform
print(platform.python_version())
    

This method is compatible with any Linux distribution (including Debian, Ubuntu, Red Hat, Fedora, Arch, Manjaro, etc.), as well as Windows, macOS, and Raspberry Pi.

Final Word

By mastering these straightforward methods, you can quickly and efficiently determine the Python version on your Linux system. Whether you need to ensure compatibility for existing programs or start a new project, knowing your Python version is an essential first step.

0 comments:

Post a Comment