How to Fix – bash: python: command not found Error

Written by: Linuxopsys   |   Last updated: June 17, 2022

The - bash: python: command not found error shows mainly because of three reasons. First of all, is the python executable installed on the machine? If it is installed, is the environment variable PATH configured correctly so it can locate the installed executable? The third reason could be a broken symlink.

This tutorial shows how to fix -bash: python: command not found error. Let's check how to solve this problem.

1. Check Python Installed

Most time this error throws on the shell prompt or command line because python not installed or got corrupted. Let's first check whether Python is installed or not.

Run the following commands to find python installed location.

which python3

or

type -a python3
Output
python3 is /usr/bin/python3
python3 is /bin/python3

or

ls -l /usr/bin/python
ls -l /usr/local/bin

Run command to check python version:

python3 --version

Simple run python3 command:

python3
Output
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

If you don't find python installed, in the next section I will show you how to install python.

Install Python on Ubuntu/Debian

Debian and Ubuntu and their derivative distributions come with python pre-installed. In case it is corrupted or not installed, use the following command.

To install python 2, type:

sudo apt install python
After January 1, 2020 Python 2 no longer receives any updates, and recommend not using it.

or

To install python 3, type:

sudo apt install python3

Python installed from source by default installed in 'http://usr/local/bin/'. In that case typing python on the console, is not going to execute the one inside the /usr/local/bin/, but the one inside the /usr/bin/.

In case you want to execute the one inside the /usr/local/bin/ you can easily configure the system by making use of an alias. The alias should be placed inside the .bashrc file, as shown below.

alias python=/usr/local/bin/python3.9

Install Python on Fedora

Thanks to the DNF package manager, you can easily install python on Fedora by:

sudo dnf install python38

Install Python on RHEL/CentOS

To install Python on RHEL, Oracle Linux and CentOS use yum command as follows:

sudo yum install python

Install Python on Arch Linux

On Arch Linux, run the following command to install python:

sudo pacman -S python2
sudo pacman -S python3

2. Check environment variable PATH

Every time you as a user run a command on your console, the machine looks for its location, or address, inside a list of predefined directories that are stored inside the environment variable PATH.

Such design helps to properly run the program or command without having to specify the absolute path on the terminal.

The environment variable PATH can be modified temporarily for the current terminal session, or permanently.

To display the content of the environment variable PATH on the console:

echo $PATH
Output
:/usr/local/opt/ruby/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Make sure the python installed path is added in the PATH variable. You can see in the above output '/usr/bin' and '/bin' paths for python 3 is present. To make it permanent, make sure to export PATH variable to ~/.bash_profile or to the respective configuration file that launches shell.

3. Check Broken Symlink

When running python script you might realize symlink to Pythons executables is wrongly pointed.

For pointing /usr/bin/python to /usr/local/bin/python3.9, type:

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

Conclusion

Through this article, we learned how to properly fix the -bash: python: command not found error on your Linux machine.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment