If you want to check the Python version on a Linux machine, it’s really not that hard.
Python is a great programming language to learn. It has simple syntax and yet it is also very powerful.
Having the latest version of the Python interpreter is a good idea. You don’t want to be using an old, out of date version, especially one that is deprecated.
How to Check Your Python Version
So, let’s get to the nitty gritty. If you are running the Python interpreter on a Linux machine, here is what you need to do to check the Python version.
Some of these commands may also work on Windows machines using the Windows command line.
Command Line Python Version Check
Login as root, and from the command line, type:
(That is two dashes before the word “version.”)
If you are using Ubuntu, and can’t login as root, use the sudo command ahead of the text as follows:
sudo python --version
The shortcut to this is to use -V instead of --version, such as:
Or, in Ubuntu:
sudo python -V
You might also try a lowercase v, because it is by the way, an almost universal command on Linux to check the version of software running on the server. However, with Python, the uppercase V is what is in the user manual.
This can be confusing because so many other programs use a lowercase v.
For example, you can use the same command with php:
Or, in Ubuntu:
sudo php -v
However, sometimes, the name of the software is not so obvious. With the Apache web server, you would think that the command might be “apache -v” but this is wrong. To find the version of Apache running on your server, type this in instead:
Or, in Ubuntu:
sudo httpd -v
HTTPD stands for “HTTP Daemon,” which makes a bit more sense – this is the daemon (or server) that uses the HTTP protocol to deliver web pages.
At any rate, if you can remember the -v, and the lowercase version is not working, you now know to try an uppercase -V for checking the Python version.
You can also just type in “python” at the command line (or “sudo python” in Ubuntu). You will get not only the version you are running, but a lot more info, such as:
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
In some respects, this is just faster and easier, and gives you a lot more information about the entire platform.
Checking the Python Version from a Control Panel
Surprisingly, when you search for ways to check the Python version from an administrative interface or control panel such as WHM or Cpanel, not much comes up.
Your best bet is to use the search function from within the control panel to look up “Python.” From there, you might be able to see which version of Python is installed and running.
Checking the Version of Python from Within a Python Script
You may want to check the version of Python from within a Python script you are writing. Some of the reasons may be that you want to ensure compatibility with the Python interpreter, especially if the script is going to be run on different servers.
There is no one solution to this, as you can find various examples and ways to do this from within Python. It will also depend on which version of Python you are writing your programming script for.
You can check sites such as Stack Overflow, where developers can get into long and sometimes convoluted discussions on how best to do certain things with code.
In this Stack Overflow discussion about checking the Python version, you can find this potential solution for looking at the version from within a Python program:
# Method 1:
assert(sys.version_info >= (2,6))
?# Method 2:
from distutils.version import StrictVersion
assert(StrictVersion(platform.python_version()) >= "2.6")
?Note that this answer does not get as many votes as some of the solutions above. However, this answer was posted on November 16, 2017, making it a much more recent answer than the highest voted answer, which was originally posted in 2009.
This is why it is important to check the dates on Stack Overflow answers, because what is voted up the most may be due to its longevity on the website, when a more current answer with fewer votes may be the answer that works.
A Little Bit More About Python Versions
Python, like any living programming language, changes over time. As the team who maintains the code improves it and optimizes it, the syntax of the language may be modified.
Usually, these changes are not so overwhelming that you couldn’t understand a Python program from a different version. However, these changes could break your program or make it not work as expected.
Compatibility Between Python Versions
Python, like PHP, is an interpreted programming language. This means that Python is not compiled into an executable program before it is run.
Instead, it is “compiled on the fly” via the Python interpreter. Thus, the program will work until it hits up against some code that isn’t in compliance with the Python version the interpreter is using. Then, the program could hiccup, mess things up, or stop working entirely.
Usually, if you are using a Python version that is the same major version, but the interpreter has a different minor version, the hiccups will hopefully be minimal. However, if you are trying to run a Python program where the major version is different from the major version of the interpreter, it is often totally incompatible.
You especially have to pay attention any time a release is labeled as “backwards incompatible.” This means that it is totally not going to work with earlier versions of the software. Python 3.0 is backwards incompatible with Python 2.0.
However, some of the features of the 3.0 version of Python have been “backported” to Python versions 2.6 and 2.7 to make the code more compatible.
Major Versions vs. Minor Versions of Python
As with most programming languages and software applications, Python uses a numerical convention to distinguish between major and minor releases.
The major version is the number before the first period. The minor version is the number after the period, with an update to that minor version following the second period:
Thus, 3.2.5 is major version 3, minor version 2.5.
Also, a “zero” indicates the general major version. All variations of Python 3 (Python 3.1.0 or 3.5.1) are part of Python 3.0. All variations of Python 2 are part of Python 2.0, etc.
Here is a list of all Python versions:
Python Beta Version
An initial version of Python was released in 1991. This was 0.9.0 and not a full number 1, as it was not ready for prime time yet. These versions were released on the following schedule:
The first official version of Python was launched in 1994.
Released in 2000, Python 2.0 not only offered a whole host of new features, but the new version was transitioned to a community-based, collaborative open source language.
Python 3.0 is also known as "Python 3000" or "Py3K," and it is the current major version of Python.
?Don’t Forget to Check Python Version
Checking the Python version is easy, just remember to do this once in a while – perhaps by placing a reminder on your calendar.
You will need to keep the Python interpreter up to date to benefit from the latest features of the ever-evolving and constantly improving Python coding language.