-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.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux or Unix
Est. reading time 3 minutes

-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/command command to find path too:
which python
type -a python
command -V python

Sample outputs:

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

First, install Homebrew on macOS and then type the following brew command:
$ brew install python3

Verify Python installation

Once installed you can view python and its version easily using any one of the following commands:
$ type -a python
$ which python
$ ls -l /usr/bin/python
$ ls -l /usr/bin/python*
$ ls -l /usr/local/bin/python*
# Make sure /usr/bin and /usr/local/bin are in PATH variable #
echo "$PATH"

Fig.02: Python installed on my OpenSUSE Linux box

On some systems, python is also known for its version. For instance python2 and python3:
type -a python
type -a python2
type -a python3
python --version # get default Python version
python2 --version # See Python 2 version
python3 --version # find Python3 version

A note about broken or missing symlink

Sometimes a soft link to Pythons’s executable is broken or missing for some reason. For example, /usr/bin/python3.4 is real executables on my AWS EC2 Linux server. You can point /usr/bin/python to /usr/bin/python3.4 for Python version 3.4 using the ln command to create soft link:
$ 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. See the following pages:

  1. UNIX / Linux: Set your PATH Variable Using set or export Command
  2. TCSH / CSH Shell Set PATH Variable on *BSD or Linux
  3. macOS/Mac OS X – Set / Change $PATH Variable
  4. Display or print UNIX / Linux path ~ $PATH variable
  5. How to add to bash $PATH permanently on Linux

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 Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
3 comments… 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

  • Anon May 23, 2021 @ 12:37

    I think it’s a ‘missing’ symlink rather than a ‘broken’ symlink!
    which python‘ failed for me but ‘which python3’ worked. This is after a new installation of ubuntu 20.04.02.
    sudo ln -s /usr/bin/python3.8 /usr/bin/python – works for me
    Try ‘ls -l /usr/bin/python*‘ first, as suggested above

  • Emmett Parks May 23, 2021 @ 21:30

    On Ubuntu 20.04 LTS we can install python-is-python3 package to get rid of this issue:
    apt info python-is-python3

    Package: python-is-python3
    Version: 3.8.2-4
    Priority: optional
    Section: python
    Source: what-is-python (4)
    Origin: Ubuntu
    Maintainer: Dimitri John Ledkov 
    Bugs: https://bugs.launchpad.net/ubuntu/+filebug
    Installed-Size: 10.2 kB
    Depends: python3
    Breaks: python, python-is-python2, python-is-python2-but-deprecated
    Replaces: python, python-is-python2, python-is-python2-but-deprecated
    Cnf-Ignore-Commands: python
    Download-Size: 2,364 B
    APT-Sources: http://archive.ubuntu.com/ubuntu focal/main amd64 Packages
    Description: symlinks /usr/bin/python to python3
     In Ubuntu, all python packages use explicit python3 or python2
     interpreter and do not use unversioned /usr/bin/python at all. Some
     third-party code is now predominantly python3 based, yet may use
     /usr/bin/python.
     .
     This is a convenience package which ships a symlink to point
     the /usr/bin/python interpreter at the current default python3. It may
     improve compatibility with other modern systems, whilst breaking some
     obsolete or 3rd-party software.
     .
     No packages may declare dependencies on this package.
    

    Then install it:
    sudo apt install python-is-python3

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum