-bash: python: command not found error and solution

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 on my Linux or Unix server?

Please note that Python version 2.7 will not be maintained past 2020. Hence, I recommend using Python version 3.x.

ADVERTISEMENTS

-bash: python: command not found

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*

We can use the which command/type command to find path too:
which python
type -a 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 my 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

python not working in command/shell prompt

Finally make sure PATH variable set correctly. If python is installed in /usr/local/bin/python PATH then run the following export command for bash/ksh/sh:
export PATH=$PATH:/usr/local/bin
Make sure you add updated path to the ~/.bash_profile.

Conclusion

In this quick tutorial you learned how to fix “-bash: python: command not found” error under Linux and Unix-like systems.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Epiphmo Jul 27, 2020 @ 4:12

    Than you for your help. My issue was a broken symlink but I have been able to fix it with this line.
    $ sudo ln -s /usr/bin/python3.4 /usr/bin/python

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.