After following the steps described in this tutorial, you will get your Python package fixed. The content below mentions all possible causes of this problem, reported by many Linux users.
All steps shown include screenshots to make it easy for every Linux user to implement them. Despite this document being based on Debian/Ubuntu Linux distributions, most steps are universal and valid for all distributions.”
The methods explained to fix the “Python command not found” error in this article include:
- Installing Python (Debian, Ubuntu, CentOS, Fedora)
- Checking the PATH variable
- Fixing Python soft/symbolic links
Note
This tutorial focuses on the Python3 package, despite it including instructions to install Python2 for users with specific needs.
Python 3 is the current version, being previous versions discontinued. If you have no specific needs with old Python versions, please only follow the steps to get Python 3 working and ignore Python 2-related commands.
Also, it is worth mentioning before continuing reading probably; you are running the python command instead of the current python3 command; if python3 works and you want it to be executed when typing python, you may want to jump to the last section of this document.
Step 1: Make Sure Python is Installed and How to Install it
The first step to diagnosing this error is to check if Python is properly installed in your system.
One of the ways to check this is by printing all Python directories under /usr/bin using a wildcard, as shown below.
Another way is by executing the command, as shown in the following screenshot.
In case the output does not show the Python version you need, on Debian/Ubuntu based systems, run the following command to update repositories prior to installation (Keep reading below for other Linux distribution instructions).
To install Python 3, run the command shown in the figure below.
Only if you are looking for Python previous version (Python 2), run the following command.
To install Python3 on CentOS, run:
And then execute:
For Python2 on CentOS, execute:
To install Python3 on Fedora, execute the following:
For Python2 run:
After installing Python, check if the command not found error persists. If it keeps showing up, try the steps described below.
Step 2: Checking the PATH Variable
If Python is installed in your system, but it is not found when executing Python commands, probably your problem is the PATH environment variable.
You must check if the path /usr/local/bin is included in the PATH variable.
To check the PATH environment variable, run the following command.
As you can see, the previous command returns paths separated by a colon. Make sure the path /usr/bin is included. If not, then execute the command shown in the figure below.
If the previous command helped and Python works, your problem was in the PATH variable. But the previously executed command is not persistent, and the path will be included only in the current session.
To solve the problem permanently, add the path to the .bashrc file under your home directory.
Open it using any text editor you want; in my case, I use nano, as shown below.
At the end of the file, add the line shown below.
Close the .bashrc file saving settings and update the path variable by running the following command.
If the PATH variable did not fix the problem, you are probably dealing with a symbolic link issue.
Step 3: Fixing Python soft/symbolic Links
In some cases, the error cause may be a wrong or absent symbolic or soft link.
The package python-is-python3 creates a symbolic link between Python and Python3 for Python3 to be executed when the user runs Python. After installing this package, running the command python will execute python3.
The first content of this section shows how to resolve this problem on Debian and Ubuntu Linux distributions by installing the mentioned package. The second section explains how to manually add the symbolic link, valid for almost every Linux distribution.
To install the python-is-python3 package on Debian or Ubuntu Linux distributions, run the following command.
Once installed, check if the soft link was properly defined by running the command shown in the following figure.
To see Python-related symbolic links, run the command described below.
Once you know the Python exact version, the syntax to add a soft link is the following:
For example, if the Python 3 version is 3.9, I would run:
Conclusion
As you can see, this common error has 3 main possible reasons. Installing Python, defining the correct path, or fixing symbolic links are solutions any Linux user can apply independently of the knowledge level. In some cases, if the error persists, try purging all Python-related packages and installing them back. In most cases, if not all, all the solutions provided in this article should fix the issue. The steps are valid for almost every Linux distribution. While some steps (Like installation) focus on Debian/Ubuntu, CentOS and Fedora, installing the same packages using your distribution packages manager is equivalent. I hope this content was useful for you to get Python working.
Thank you for reading this tutorial explaining how to fix the error “Python command not found” in Linux. Keep following us for more professional articles.