Categories: TutorialsUbuntu

How to Check Python Version in Linux (via 3 Methods)

Python is one of the most widely used programming languages that powers many popular web applications, AI/ML technologies, IoT devices, task automation, and many more.

Sponsored

Right now, Python 3 is the latest version, but if you are using an outdated system, you might be using an older version of Python, possibly Python 2, which is now deprecated.

Knowing the correct Python version is crucial, whether you are running a program that only works with a specific version or planning to start a Python project with the latest version.

In this quick guide, I’ll show you how to check the Python version in Ubuntu and other Linux distros from the command line.

How to Check Python Version in Linux (via 3 Methods)

There are multiple ways to find out the Python version from the command line; here I’ve provided you with the first two ways to identify if Python 2 or 3 is running on your Linux system, and the third method works for any Python version.

Method 1: Check Python 2 Version via Python Command

If you are running the older Python 2 version (which is now deprecated) on your Linux system, you can try running the “python” command with the “-V” or “–version” option to check its version information.

$ python -V

# OR

$ python --version

Output:

This method only works for Python 2, as Python 3 requires a different command, which you can find in the next section.

Method 2: Check Python 3 Version via Python Command

To check the Python 3 version information, you can try running the “python3” command with the “-V” or “–version” option.

$ python3 -V

# OR

$ python3 --version

Output:

This method works for any Linux distribution, as well as for Windows and macOS.

Sponsored

Method 3: Check Python Version via Python Interpreter

The previous two methods will help you find the Python version installed on your Linux system, but if you want to check the Python version in the interpreter (also known as the Python console) or within a Python script, you can do that too.

The “sys” and “platform” modules can help you check the installed Python version; the “sys” module provides detailed information, including the build number and date, while the “platform” module only prints the version.

You can use any one of them according to your needs, so when you’re ready, open your Python console or script and copy-paste any 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())

Output:

This method would work on any Linux distribution (such as Debian, Ubuntu, Red Hat, Fedora, Arch, Manjaro, etc.), as well as on Windows, macOS, or Raspberry Pi.

Final Word

Knowing the correct Python version is crucial to ensuring compatibility among the program, libraries, framework, and applications. This article shows you various methods to check the installed Python version on your Linux system, whether from the command line, the Python interpreter, or programmatically.

If you have any questions, feel free to ask them in the comments.

Till then, peace!

The post How to Check Python Version in Linux (via 3 Methods) appeared first on Linux TLDR.

Ubuntu Server Admin

Recent Posts

How to Fix VMware’s “Could not open /dev/vmmon” Error on Ubuntu

You’ve recently installed VMware Workstation on your Ubuntu system and encountered the frustrating “Could not…

5 hours ago

How to Fix Ubuntu 404 Errors While Fetching Dependencies

Have you ever found yourself staring at a terminal full of 404 errors while trying…

5 hours ago

How to Fix ‘Please Install All Available Updates’ Error When Upgrading Ubuntu 18.04 to 20.04 LTS

One particularly frustrating error that many users face when trying to upgrade from Ubuntu 18.04 …

5 hours ago

How to fix “Release is not valid yet” Error in Docker Containers

In the world of containerization, time synchronization issues can create unexpected roadblocks when working with…

5 hours ago

How to fix “Externally Managed Environment” Pip Errors on Ubuntu

If you’ve recently upgraded to Ubuntu 23.04 or newer, you might have encountered a frustrating…

5 hours ago

Ubuntu now officially supports NVIDIA Jetson: powering the future of AI at the edge

Canonical announces the General Availability of Ubuntu for the NVIDIA® Jetson Orin™ for edge AI…

12 hours ago