How to Check Python Version in Linux
Checking your Python version in Linux is a common task for developers and system administrators. Whether you’re setting up a new environment or troubleshooting, knowing which Python version is installed helps you avoid compatibility issues. In this guide, I’ll walk you through easy methods to find out your Python version on any Linux system.
You don’t need to be a Linux expert to follow along. I’ll explain commands step-by-step and offer tips for different Linux distributions. By the end, you’ll be confident in verifying your Python setup quickly and accurately.
Why Checking Python Version Matters
Python is a versatile programming language used in many projects. However, different projects may require different Python versions. For example, Python 2 and Python 3 have significant differences, and some software only works with one or the other.
Here’s why you should always check your Python version:
- Compatibility: Ensure your code or tools run smoothly without errors.
- Environment Setup: Confirm the right Python version is installed before starting development.
- Troubleshooting: Identify version-related issues when debugging scripts.
- Multiple Versions: Manage multiple Python installations on the same system effectively.
Knowing your Python version helps you avoid surprises and keeps your projects running smoothly.
How to Check Python Version Using the Terminal
The most straightforward way to check your Python version in Linux is through the terminal. Linux systems come with a terminal or shell where you can type commands directly.
Using python Command
Open your terminal and type:
python --version
or
python -V
This command will display the installed Python version, such as:
Python 2.7.18
If you see an error like “command not found,” it means Python 2 is not installed or not linked to the python command.
Using python3 Command
Most modern Linux distributions use Python 3 as the default. To check Python 3, type:
python3 --version
or
python3 -V
You might see output like:
Python 3.11.2
This confirms Python 3 is installed and available.
Why Use python3 Instead of python?
Many Linux systems still link python to Python 2 for backward compatibility. Since Python 2 reached end-of-life, most new projects use Python 3. Always check both commands if you’re unsure which version is active.
Checking Python Version with which and whereis
Sometimes, you want to know the exact location of the Python executable on your system. This helps when managing multiple Python versions or virtual environments.
Using which
Type:
which python
or
which python3
This command shows the full path of the Python executable, for example:
/usr/bin/python3
Using whereis
Another option is:
whereis python
This lists all locations related to Python binaries and documentation:
python: /usr/bin/python3.11 /usr/bin/python /usr/lib/python3.11 /usr/include/python3.11
Knowing the path helps you manage or switch between Python versions.
Checking Python Version Inside a Script
If you want your Python script to check the version programmatically, you can use the sys module.
Here’s a simple example:
import sys
print("Python version")
print(sys.version)
print("Version info.")
print(sys.version_info)
Running this script will output detailed version information, including major, minor, and micro versions.
This method is useful when you want your program to adapt based on the Python version it’s running on.
Using Package Managers to Check Python Version
Linux distributions use package managers to install and manage software. You can check Python versions through these tools as well.
Debian/Ubuntu (APT)
Run:
apt list --installed | grep python
This lists installed Python packages and their versions.
Or check a specific package:
apt show python3
Red Hat/CentOS/Fedora (YUM/DNF)
Use:
dnf list installed | grep python
or
yum list installed | grep python
This shows installed Python packages and versions.
Arch Linux (Pacman)
Try:
pacman -Qs python
This searches for Python packages and their versions.
Using package managers helps confirm which Python versions are installed system-wide.
Managing Multiple Python Versions on Linux
Sometimes, you may need to switch between different Python versions. Linux allows multiple versions to coexist, but you need tools to manage them.
Using update-alternatives
On Debian-based systems, update-alternatives lets you configure the default Python version.
To see current alternatives:
sudo update-alternatives --config python
If Python is not set up here, you can add versions and switch between them.
Using pyenv
pyenv is a popular tool to install and manage multiple Python versions easily.
To install:
curl https://pyenv.run | bash
After installation, you can list available versions:
pyenv install --list
Install a specific version:
pyenv install 3.10.9
Set a global or local Python version:
pyenv global 3.10.9
pyenv local 3.10.9
pyenv is very useful for developers working on projects requiring different Python versions.
Troubleshooting Common Issues When Checking Python Version
Sometimes, checking your Python version might not go as expected. Here are common problems and solutions:
- Command Not Found: If
pythonorpython3commands don’t work, Python might not be installed or not in your PATH. - Wrong Version Displayed: Your system might have multiple Python versions. Use
which pythonto find the active one. - Permission Denied: Run commands with
sudoif you face permission issues. - Virtual Environments: If you use virtual environments, the Python version inside may differ from the system version. Activate the environment first.
If you encounter issues, verifying your PATH variable and installed packages usually helps.
Summary Table: Commands to Check Python Version in Linux
| Command | Description | Example Output |
python --version | Check Python 2 version | Python 2.7.18 |
python3 --version | Check Python 3 version | Python 3.11.2 |
which python | Find path of Python 2 executable | /usr/bin/python |
which python3 | Find path of Python 3 executable | /usr/bin/python3 |
apt list --installed | List installed Python packages (Debian/Ubuntu) | python3/3.11.2-1ubuntu |
dnf list installed | List installed Python packages (Fedora) | python3-3.11.2-1.fc36 |
pyenv versions | List Python versions managed by pyenv | system, 3.10.9 |
This table helps you quickly pick the right command for your needs.
Conclusion
Now you know several simple ways to check the Python version on your Linux system. Whether you prefer using the terminal commands like python --version or tools like pyenv, you can easily confirm which Python version is active.
Checking your Python version regularly helps you avoid compatibility problems and manage your development environment better. Keep these commands handy, and you’ll be ready to handle any Python-related task on Linux with confidence.
FAQs
How do I check if Python 3 is installed on Linux?
Open the terminal and type python3 --version. If Python 3 is installed, it will display the version number. Otherwise, you’ll get a command not found error.
Can I have both Python 2 and Python 3 on Linux?
Yes, Linux supports multiple Python versions. You can use python for Python 2 and python3 for Python 3, or manage versions with tools like pyenv.
What command shows the Python executable path?
Use which python or which python3 to find the full path of the Python executable on your system.
How do I check Python version inside a script?
Import the sys module and print sys.version or sys.version_info to get detailed version information programmatically.
What if python command shows the wrong version?
Your system might default python to Python 2. Use python3 to access Python 3, or configure alternatives with update-alternatives to change the default.
