How to Resolve Python Command Not Found Error in Linux

Python Command Not Found

Python, with its approachable syntax, straightforward semantics, and extensive lineup of frameworks and libraries, is often the preferred solution for data science projects and scripting requirements. It finds widespread applications in various fields, such as web development, analytics, machine learning, and data science.

However, many users encounter the python command not found error in a Linux environment, especially when they freshly set up a Python environment. 

What Leads to the python: command not found Error?

In general, this error occurs when the operating system fails to locate the Python executable file and thus cannot execute Python commands or scripts.

The most common reasons for getting the error are:

  •  Python isn’t installed on your computer.
  •  The PATH setting doesn’t point to where Python is located.
  •  The Python shortcut or link isn’t set up correctly.

To resolve the python: command not found error:

  • Check if Python is installed (Install it if it’s missing).
  • Update where Python is located by adjusting the PATH.
  • Make sure the correct Python version is used by adding or adjusting symbolic links.

How to Resolve python: command not found Error in Linux

We’ll now discuss the details of resolving this critical error. We’ll start with a short overview of installing Python on popular Linux distributions because the error pops up when the system can’t find an active Python installation.

Let’s see the prerequisites of the process:

Prerequisites

  • Terminal or command line access.
  • A user account with root or sudo privilege.

Step #1: Install Python

The quickest way to see if Python is on your computer is to check the version information. For this, just type this command in the terminal:

# python3 –version.

Installation of Python

The terminal shows the Python version number if Python is installed and available on your system. In this instance, the Python version is displayed as 3.6.8.

Alternatively, if Python is not installed on your machine, the version command will fail, and you will see the python: command not found error message.

Python Installation

Python 3 Commands

How to Install Python on CentOS

To install Python on CentOS, follow these steps:

Open the command line and use the dnf package manager to update the list of packages in CentOS:

# yum update

Install python on centos

Next, use this command to install Python 3 on your CentOS system:

# yum install python3 -y

yum install python3

How to Install Python on Rocky Linux

To set up Python on Rocky Linux, follow these steps:

Launch a terminal window and refresh the list of packages in Rocky Linux:

#dnf update -y

dnf update -y

Next, use this command to install Python 3 to your Rocky Linux system:

#dnf install python3 -y

dnf install python3 -y

Next, verify the Python installation by checking the version information with the following command: 

#python3 –version

python3 --version

How to Install Python on Ubuntu

To get Python on Ubuntu, launch the terminal and refresh the list of Ubuntu packages:

# apt update -y

apt update -y

Next, run this command to add Python 3 on Ubuntu:

#apt install python3 -y

app install python3 -y

Verify the Python version to make sure it’s installed correctly.

# python3 –version

python3 --version

How to Install Python on Debian

To install Python on Debian, follow these steps:

Open the terminal and refresh the list of Debian packages.

#apt-get update 

apt-get update

Next, run the following command to install Python 3 on your Debian system: 

# apt install python3 -y

apt install python3 -y

Make sure Python is installed correctly by opening the Python 3 interactive shell.

python 3.11.2

To leave the Python shell, press Ctrl+D.

 

How to Install Python on ArchLinux

To install Python on Arch Linux, you can use the package manager Pacman, which makes the process quite straightforward. Here are the steps to install Python on Arch Linux:

Launch the terminal and update the package index to ensure you have the latest information about available packages. 

# pacman -Syu

Next, install Python with the following command: 

# pacman -S python

This will install the latest version of Python 3 available in the Arch Linux repositories.

After the installation is complete, you can verify the Python installation by running the following command:

# python –version

How to Install Python on Fedora

Installing Python on Fedora is a straightforward process using the DNF package manager, which is the default package manager on Fedora. 

Launch the terminal and update the package index with the following command: 

# dnf update

Next, install Python 3 with the following command:

# dnf install python3

This command will install the latest version of Python 3 available in the Fedora repositories.

After the installation is complete, you can verify the Python installation by running the following command:

# python3 –version

Step #2: Verify the PATH Variable

The PATH is like a list of places the computer looks for programs. It uses this location as an index of the actual program location. After installation, you should make sure that the PATH contains an entry for Python. 

For this, print out the PATH contents with the following command:

# echo $PATH.

The result should show the location where Python is installed.

echo $PATH

If the computer doesn’t know where Python is, you can tell it where to find it by manually adding the path to Python’s installation.

For this, open the .bashrc file in your preferred text editor. We’ll use vi for this demonstration: 

# vi ~/.bashrc

Add this line to the end of the .bashrc file:

export PATH=”$PATH:/python/path/executable/”

.bashrc

Swap /python/path/executable/ with the actual directory path where Python is located on your computer.

Save the file and then close the editor.

Next, use the following command to make the changes take effect:

# source ~/.bashrc

As a good measure, close and reopen the terminal.

To double-check if the PATH is updated, use the echo command:

# echo $PATH

echo $PATH

Step #3: Verify Python Symbolic Links

Symbolic links, or symlinks, are like signposts that guide to other files or folders. They’re handy for making shortcuts or nicknames for Python programs.

When you have several Python versions, symlinks help you decide which one to use when you type a command.

You can view the list of Python symlinks with the following command:

# ls -l /python/path/executable.

Put in the actual path to your Python folder instead of /python/path/executable

This command shows information about the Python program file and shows shortcut links. For instance, if files are in the ./python3.11 folder, you can use python3 to run them.

verify python symbolic links

You should make a new symlink to allow users to use the python command for running Python 3.

# ln -s /usr/bin/python3 /usr/bin/python

Similarly, you can delete symlinks that are not being used or broken with the rm command:

# rm /usr/bin/python

Conclusion

You might see python: command not found error, especially if you have freshly installed Python on a system, or the PATH doesn’t contain the right information. 

The best way of avoiding this error is to install Python the right way is the easiest way of avoiding this error. We covered the proper process of installing Python on all popular Linux distributions. 

We also discussed the critical steps about setting the PATH and symlinks to ensure smooth performance for Python applications.

If you’re looking for a robust server for your Python projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the cause of the python: command not found error in Linux Ubuntu?

The python: command not found error occurs when the system cannot find the Python interpreter in the specified path.

Q. How do I install Python on Linux Ubuntu?

To install Python on Linux Ubuntu, you can use the following command: sudo apt-get install python

Q. How do I check if Python is installed on my system?

You can check if Python is installed on your system by typing the following command in your terminal: python –version

Q. What should I do if I see the python:  command not found error?

To resolve the python: command not found error in Linux, you can try the following steps:

  1. Check if Python is installed by running the command python –version
  2. If Python is not installed, install it using the command sudo apt-get install python
  3. If Python is already installed, check the PATH variable to ensure it includes the directory where Python is installed.
  4. If the PATH variable is correct, check if there is a broken symlink for Python by running the command ls -l $(which python)
  5. If there is a broken symlink, remove it by running the command sudo rm $(which python) and then create a new symlink using the command sudo ln -s $(which python3) /usr/bin/python

Q. How do I configure the Python PATH variable in Linux Ubuntu?

To configure the Python PATH variable in Linux Ubuntu, you can follow these steps:

  1. Open the terminal
  2. Edit the ~/.bashrc file using a text editor
  3. Add the following line to the file: export PATH=$PATH:/path/to/python (replace /path/to/python with the actual path to your Python installation)
  4. Save the file and exit the text editor
  5. Run the command source ~/.bashrc to apply the changes

Q. How can I check the Python path on Linux Ubuntu?

You can check the Python path on Linux Ubuntu by executing the command in python

Q. Can I use Python 3 instead of Python 2?

Yes, you can use Python 3 instead of Python 2. In recent versions of Ubuntu (e.g., Ubuntu 18.04 and Ubuntu 20.04), the Python command is linked to Python 3 by default.

Q. How do I fix the python: command not found error in Ubuntu 18.04?

To fix the python: command not found an error in Ubuntu 18.04, you can use the following command to create a symlink: sudo update-alternatives –install /usr/bin/python python /usr/bin/python3 1

Q. What should I do if Python is installed but still getting the python: command not found error?

If Python is installed, but you still get the python: command not found error, you can try using the command python3 instead of python to execute your Python scripts.

Q. How do I run Python commands in Linux?

To run Python commands in Linux, open a terminal and type python or python3 (depending on your installed version). This will launch the Python interpreter, where you can execute your commands.

Jignesh J

Jignesh is a senior server administrator at RedSwitches. He keeps everything up & running while tackling advanced server management and high-availability cluster issues. He’s a big fan of blockchain and web security. When not at his terminal, he loves to work out and is a fitness freak. If you have any support issues, contact him at [email protected]

Related articles

Latest articles