Linux / Unix: “-bash: python: command not found” error and solution

Posted on in Categories , , , , , , , , , , , last updated January 11, 2017

I am a new user and trying to run Python program. I have a cloud based VM/VPS and when I type python mycode.py at the terminal of my server, I get the following error:
-bash: python: command not found

How do I solve this problem?

This error means Python is either not installed or your installation damaged. Here is how you can solve this problem.

Check for python path

Type any one of the following commands to see if python binary exists on a Linux or Unix-like system:
type -a python

OR
ls -l /usr/bin/python
ls -l /usr/bin/python*

OR
which python
Sample outputs:

Fig.01: Python command not found
Fig.01: Python command not found

It seems that Python is missing for an unknown reason or was not installed by your cloud provider to save the disk space. So install it as per your Linux distro or Unix variant:

Ubuntu/Debian/Mint Linux install Python

Type the following apt-get command or apt command
$ sudo apt-get install python
Or install python version 3:
$ sudo apt-get install python3

Oracle/RHEL (Red Hat)/CentOS Linux install Python

Type the following yum command:
$ sudo yum install python

Fedora Linux install Python

Type the following dnf command to add the Python v2.x:
$ sudo dnf install python
OR to add the Python v3.x:
$ sudo dnf install python3

Arch Linux install Python

Type the following pacman command to add the Python v2.x:
$ sudo pacman -S python2
OR add the Python v3.x:
$ sudo pacman -S python3

Suse/OpenSUSE Linux install Python

Type the following zypper command to add the Python v2.x:
$ sudo zypper install python
OR add the Python v3.x:
$ sudo zypper install python3

FreeBSD Unix install Python

Type the following pkg command to add the Python v2.x:
# pkg install python2
OR To install the Python v2.x port:
# cd /usr/ports/lang/python2/ && make install clean
To add the Python v3.x package:
# pkg install python3
OR To install the Python v3.x port:
# cd /usr/ports/lang/python3/ && make install clean

OpenBSD Unix install Python

Type the following pkg_add command to add the Python v2.x or 3.x:
# pkg_add python
OR
$ doas pkg_add python
Sample outputs:

quirks-2.241 signed on 2016-07-26T16:56:10Z
Ambiguous: choose package for python
a       0: 
        1: python-2.7.12
        2: python-3.4.5
        3: python-3.5.2
Your choice: 

MacOS X Unix install Python3

Type the following command:
$ brew install python3

Verify Python installation

Type the following commands:
$ type -a python
$ which python
$ ls -l /usr/bin/python
$ ls -l /usr/bin/python*

Sample outputs:

Fig.02: Python installed on my OpenSUSE Linux box
Fig.02: Python installed on my OpenSUSE Linux box

A note about broken symlink

Sometimes a soft link to Pythons’s executables is broken for some reason. For example, /usr/bin/python3.4 is real executables. You can point /usr/bin/python to /usr/bin/python3.4 for Python version 3.4 using the ln command:
$ sudo ln -s /usr/bin/python3.4 /usr/bin/python
Now you can run program:
$ python mycode.py

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.