Fixing the “env: python: No such file or directory” Error in Linux

The “env: python: No such file or directory” error in Linux indicates that the system cannot find the Python executable when you try to run it. This usually happens when the Python path is not set properly. Here are some ways to fix this issue:

What Causes This Error?

When you execute a command like python or python3 in Linux, the system searches through the directories listed in the PATH environment variable to locate the Python executable file. If Python is not installed in any of those directories, or if the PATH is not set correctly, you will see the “no such file or directory” error.

Some common reasons why this happens:

  • Python is not installed on your system
  • Python is installed but the directory containing Python is not in the PATH
  • PATH is set incorrectly so the system is searching for the wrong directories
  • Symbolic links to Python executables are broken

Fix 1 – Install Python

If python is not present on your Linux distribution at all, install it using your distribution’s package manager.

On Debian/Ubuntu:

sudo apt update
sudo apt install python3
JavaScript

On Fedora:

sudo dnf check-update
sudo dnf install python3
JavaScript

On Arch Linux/Manjaro:

sudo pacman -Syu
sudo pacman -S python
JavaScript

This will install python and add it to your system’s PATH.

Fix 2 – Add Python Directory to PATH

If python is already installed but the directory containing the python executable is not in PATH, add that directory to PATH.

First find out where python is installed:

# For python3
which python3

# For python2
which python2
JavaScript

This will print the full path to the python executable, for example /usr/bin/python3.

Now add that directory to PATH, either temporarily for the current session:

export PATH="$PATH:/usr/bin"
JavaScript

Or permanently for all future sessions by adding this line to your ~/.bashrc or ~/.profile:

export PATH="$PATH:/usr/bin"
JavaScript

Fix 3 – Recreate Symbolic Links

Sometimes the symbolic links pointing to the python executable can get broken, causing the “no such file or directory” error.

To check if the symbolic links are broken, run:

ls -l /usr/bin/python*
JavaScript

This will show all python symlinks. If any of them shows a red broken link, recreate that symlink:

sudo ln -sf /usr/bin/python3.6 /usr/bin/python3
JavaScript

Substitute python3.6 with the actual python version on your system.

Fix 4 – Reinstall Python

In rare cases, you may need to fully reinstall python to fix the issue.

First uninstall python:

On Debian/Ubuntu

sudo apt remove python3
JavaScript

On Fedora

sudo dnf remove python3
JavaScript

Then install it again using the same commands provided earlier.

This will overwrite any existing broken symlinks and PATH issues.

Fix 5 – Use the Full Path

As a workaround, you can always invoke the python executable by its full path instead of just python.

For example:

/usr/bin/python3 script.py
JavaScript

Or:

/usr/local/bin/python script.py
JavaScript

This eliminates the need for python to be in PATH and avoids the “no such file or directory” error.

Conclusion

The “env: python: No such file or directory” error occurs because the linux system cannot find the python executable. It can be fixed by properly installing python, adding its directory to PATH, recreating broken symlinks, reinstalling python completely, or using the full path to the Python executable.

Following these troubleshooting steps will resolve the issue and allow you to run Python normally again. Fixing PATH environment variables is key to resolving this and many other command-not-found errors in Linux.

Leave a Comment