Linux Commands

Fix Python Command Not Found Error in Linux

This article provides instructions to fix the error “Python command not found” in Linux.

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.

ls /usr/bin/python*

Another way is by executing the command, as shown in the following screenshot.

which python python2 python3

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).

sudo apt update

To install Python 3, run the command shown in the figure below.

sudo apt install python3 -y

Only if you are looking for Python previous version (Python 2), run the following command.

sudo apt install python2 -y


To install Python3 on CentOS, run:

sudo yum update

And then execute:

sudo dnf install python3

For Python2 on CentOS, execute:

sudo yum install python2

To install Python3 on Fedora, execute the following:

sudo dnf install python3

For Python2 run:

sudo dnf install -y python2

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.

echo $PATH

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.

export PATH=$PATH:/usr/bin

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.

nano .bashrc

At the end of the file, add the line shown below.

export PATH="$PATH:/usr/bin"

Close the .bashrc file saving settings and update the path variable by running the following command.

source .bashrc

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.

sudo apt install python-is-python3

Once installed, check if the soft link was properly defined by running the command shown in the following figure.

ls -l /usr/bin/python


To see Python-related symbolic links, run the command described below.

ls -l /usr/bin/python*

Once you know the Python exact version, the syntax to add a soft link is the following:

sudo ln -fs /usr/bin/python<Version> /usr/bin/python

For example, if the Python 3 version is 3.9, I would run:

sudo ln -fs /usr/bin/python3.9 /usr/bin/python

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.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.