When working with Python on a Linux system, it’s important to know which Python versions are installed and in use. This knowledge can help you solve compatibility issues, ensure specific features are available, and maintain a clean environment. In this tutorial, we will walk through the steps to check if multiple Python versions are installed in your Linux system.
Step 1: Check the default Python version
Open a terminal window and type the following command to check the default Python version:
1 |
python --version |
The output will display the Python version as follows:
Python 3.8.5
You can also run the following command to check the default version of Python 3:
1 |
python3 --version |
Step 2: Check for other installed Python versions
Next, we can check if there are other installed versions. Type the following command to list all the available Python executables in your system:
1 |
ls /usr/bin | grep python |
The output will be similar to this:
python python2 python2.7 python3 python3.6 python3.6m python3.8 python3.8-config python3-config python-config
This example output shows that the system has Python 2.7, Python 3.6, and Python 3.8 installed.
Step 3: Use a specific Python version
If you need to use a specific Python version for a project or script, you can specify the version when running the script. For example, if you have a script named example.py
and you want to run it with Python 3.6, you can use the following command:
1 |
python3.6 example.py |
Step 4: Change the default Python version (Optional)
If you want to change the default Python version for your system, you can use the update-alternatives
command. First, check the available alternatives by typing:
1 |
update-alternatives --list python |
If no alternatives are listed, you can add the installed Python versions as follows:
1 2 |
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2 |
Now, you can use the following command to change the default Python version:
1 |
sudo update-alternatives --config python |
Select the desired version from the list shown, and the default Python version will be updated.
Conclusion
In this tutorial, we went through the steps of checking for multiple Python versions installed in your Linux system and discussed how to change the default Python version. Knowing which Python versions are available and how to switch between them will help you maintain a productive development environment and avoid potential issues.