Fixing “bash: python: command not found” Error in Linux

Seeing the “bash: python: command not found” error in Linux terminal is frustrating, especially when trying to run Python scripts or applications. This comprehensive guide explains what causes this error and the step-by-step methods for fixing it on any Linux distribution.

What Causes the “python: command not found” Error?

The “command not found” error occurs when the system is unable to locate the executable file for a program you are trying to run. There are two main reasons this happens with the python command:

1. Python is not installed

Python does not come pre-installed on Linux. The python command allows running the Python interpreter or scripts. If Python is not installed, bash cannot find the python executable file to launch it.

2. Python executable path not in $PATH

Even if Python is installed, the directory containing the python executable must be present in the $PATH system variable. $PATH indicates directories search for commands executed. If missing, the python command is unrecognized.

Below we look at solutions to handle both these scenarios – installing Python and updating $PATH to resolve the error for good.

Method 1 – Install Python

If Python is not already installed, you need to install it first before being able to run the python or other Python commands.

Step 1 – Check Python Version Installed

First check if you already have Python but it’s still not working. Type:

python3 --version

If this shows a Python version (e.g. Python 3.8.2), then Python is installed but the executable path requires updating – jump to method 2.

If it returns “command not found”, Python is missing completely.

Step 2 – Install Python

Next install Python using your Linux distribution’s package manager:

Debian/Ubuntu

sudo apt update
sudo apt install python3

Fedora

sudo dnf check-update
sudo dnf install python3

Arch Linux

sudo pacman -Syu
sudo pacman -S python

openSUSE

sudo zypper refresh
sudo zypper install python3

This will install the latest Python 3 version and all required dependencies.

Step 3 – Confirm Python Version

Check it installed correctly:

python3 --version

The Python version will now be displayed.

With Python installed, try running the python command again – this should work directly unless $PATH needs fixing as well.

Method 2 – Add Python to $PATH

If Python is installed but bash still can’t find the command, the next step is adding the Python executable path to the $PATH environment variable.

On Linux, Python 3 installs to /usr/bin/python3 – but $PATH may not contain this path.

Here is how to add it:

Step 1 – Find Python Executable Path

Find where python3 is installed by running:

which python3

This prints the full path to the Python 3 executable file.

E.g. /usr/bin/python3

Step 2 – Get Current $PATH value

Check your current $PATH variable value:

echo $PATH

This lists all paths separated by a colon that are searched when you execute a command.

A typical $PATH is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

/usr/bin/python3 needs to be included in $PATH for the python command to work.

Step 3 – Add Python Path to $PATH

You can modify $PATH just for your current session:

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

However this change is lost when you close the terminal.

To make it permanent, add the path to your shell profile config file:

Bash – Add to ~/.bashrc:

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

Zsh – Add to ~/.zshrc:

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

Now source the file to update the current shell:

source ~/.bashrc

OR

source ~/.zshrc

Python’s executable location is now permanently added to $PATH

Step 4 – Confirm with python Command

Finally, confirm Python can now be launched correctly:

python --version

The command should run successfully – python: command not found error resolved!

With this $PATH modification, all Python commands like python, pip, python3 etc will be accessible in Bash.

An alternative to updating $PATH is to create a symbolic soft link to the python3 executable in a directory already in $PATH, like /usr/local/bin.

Run this command to symlink python3:

sudo ln -s /usr/bin/python3 /usr/local/bin/python

This creates a virtual python soft link pointing to the real python3 executable.

Step 2 – Run Python Command

Now test running Python via the link:

python --version

With the link in /usr/local/bin present in $PATH, the python command maps to python3 and works normally.

This avoids modifying $PATH but achieves the same effect. Generally updating $PATH directly is the cleaner approach.

Troubleshooting Python Command Not Found Issues

If you still get “python: command not found” after the above steps, a few things to check:

Following this troubleshooting guide helps narrow down the cause and returns the python command up and running on any Linux distribution.

Conclusion

The “python: command not found” error occurs when the Linux shell is unable to locate the Python executable file to launch the command. Installing Python if missing and adding its path to the $PATH system variable are the key solutions discussed in detail.

Here is a summary of the main steps to permanently fix this error:

With Python accessible, you are ready to start running Python scripts and applications smoothly from the Linux terminal and integrate it into various workflows. Troubleshooting any further issues is also easier when the fundamentals of $PATH resolution are understood.

Leave a Comment