There are multiple ways to check python version on Ubuntu 24.04. Python 2 has become obsolete as it reached its end of life in the year 2020, so it does not come pre-installed on Ubuntu 24.04. Python3 comes pre-installed on Ubuntu latest releases.Â
In this article, we have specifically explored ways to check python version on Ubuntu 24.04. You can read this article to know how to check your python version on Windows and Mac.
1. How to Check Python Version Using Terminal
You can check which version of python3 is installed on Ubuntu 24.04 by running following command:
python3 --version
You can also check the version by this command:
python3 -V
2. How to Check Python Version Using Python Interpreter
Alternatively, you can check python version by using the python interpreter. Enter into python interpreter environment by typing following in your terminal:
python3
You can now import sys and find out version information by printing it:
import sys
print(sys.version)
Alternatively, you can import platform and also check out the version by printing it using built-in function python_version():
import platform
print(platform.python_version())Â Â
You can exit the python interpreter like this:
exit()
3. How to Check Python Version Using which Command
Nothing fancy, it’s very simple to check python version using which command:
which python3
4. How to Check Python Version Using ls Command
Again, nothing complicated just a simple ls command with a path to track all python executables within /usr/bin directory:
ls /usr/bin/python*
5. How to Check Python Version Using a Python Script
You can also check the version of python on your Ubuntu by writing a python script. There are some easier ways to do the same thing like writing a script that uses ‘sys’ or ‘platform’ to simply print the python version but I am going to do it differently. I’ll explain to you step by step what I am doing and in the end will share the complete script with comments.
Step 1: Create a python script with .py extension using terminal:
touch python_hi.py
Open this script file in your nano editor:
nano python_hi.py
Step 2: On the very first line, write shebang and then import ctypes:
#!/usr/bin/env python3
Import ctypes
ctypes is a built-in python module which helps python code interact with C programming functions and that also means you don’t have to write full C code to use the efficacy of C programming to some extent.
Step 3: In the next line, you need to create a variable that will hold the object of python interpreter’s shared library that gives us the ability to be able to call C functions:
py_c_lib = ctypes.PyDLL(None)
‘py_c_lib’ is a variable, ‘PyDLL’ is a class of ctypes module that helps you load the shared library of python interpreter with C and argument ‘None’ loads the shared library of python interpreter as python interpreter is currently running the script.
Step 4: Now you need to basically make sure that your script works well with the C API functions of python interpreter for that write:
py_c_lib.Py_GetVersion.restype = ctypes.c_char_p
We need to convert C pointer into something python could understand so for that matter, ctypes.c_char_p what it returns is converted into python compatible bytes object using Py_GetVersion.restype, restype converts the C data types.
Step 5: So now, we will use Py_GetVersion() function to get the version string in the form of bytes object and store it into a variable named as ‘version_bytes’ and later we could convert this into a string from a python byte object:
 version_bytes = py_c_lib.Py_GetVersion()
Step 6: Finally, all we need to do is to convert the version string from byte object to a readable normal python string and then print it:
print(version_bytes.decode('utf-8'))
It uses utf-8 encoding to translate the byte object into a readable string format.
Complete Script with Comments for Understanding
#!/usr/bin/env python3
import ctypes
#load the python shared library in the script
py_c_lib = ctypes.PyDLL(None)
#set the return type of Py_GetVersion() to a pointer to a character array so its python compatible
py_c_lib.Py_GetVersion.restype = ctypes.c_char_p
#call the C function Py_GetVersion to get the python version string
version_bytes = py_c_lib.Py_GetVersion()
#decode using utf-8 and print the version
print(version_bytes.decode('utf-8'))
Step 7: Now, Save the script by pressing ‘CTRL+O’ in your nano editor and exit it using ‘CTRL+X’ and after that make your script executable by writing the following command in the terminal:
chmod +x python_hi.py
Step 8: Go ahead and run your python script:
./python_hi.py
Conclusion
We explored multiple ways essentially to do the same thing i.e. to check python version on Ubuntu 24.04 LTS. We have used which command, ls command, python interpreter, and wrote a python script that uses C functions API to check the version of python.
Check our LinkedIn company page