In this guide, we’ll walk you through various methods to help you check Python version on Linux. Python has become one of the most popular programming languages worldwide, because of its simplicity and the wide range of areas in which it is used such as web frameworks, software development, data analysis, and task automation. However, ensuring you have the correct version of Python installed is crucial to check for compatibility and if you are running the latest version if you are starting a new project.
Check Python Version on Linux Using Command Line
The simplest and most common way to check the Python version is through the command line or terminal. Here’s how you can do it:
First, you will need to log in to your Server via SSH:
# ssh jeff@IP_Address -p Port_number
You will need to replace ‘IP_Address’ and ‘Port_number’ with your server’s respective IP address and SSH port number.
For the older Python version, for Python 2 installations, you can type the following command in the terminal:
python --version
You can also check the Python 2 version with the command:
python -V
This will display the installed Python version, such as Python 2.7.18.
To check the version the installed Python3 version, open your terminal or command prompt and type the following command:
python3 --version
or
python3 -V
This will display the installed Python 3 version, such as Python 3.8.5.
Checking Python version using the Python Interpreter
Another way to check the Python version is by using the Python interpreter itself also known as the Python shell. Here’s how:
Open your terminal or command prompt and for Python 2 and Python 3 both, type python and click enter.
Once in the Python interpreter, import the sys module with:
import sys
and then enter the following command:
print(sys.version)
This will print detailed information about the Python version, including the build number and date.
If you want to check the Python version within a Python script, you can use the following method:
import platform
Import the platform module and then print the Python version using the command or line:
print(platform.python_version())
This will output the Python version installed on your system.
Knowing how to check the Python version is essential for ensuring compatibility with libraries, frameworks, and applications. By following the methods outlined in this guide, you can easily determine the Python version installed on your machine, whether you’re using the command line, Python interpreter, or programmatically within a script.
Keeping your Python environment up-to-date is crucial for staying current with the latest features, security patches, and improvements in the language. If you like this post, please share it with your friends if they need help on how to check the Python version on their system.