Amazon Fire TV Stick HD (newest model), free and live TV, Alexa Voice Remote, smart home controls, HD streaming
48% OffSeeing the “bash: python: command not found” error in Linux terminal is frustrating, especially when trying to run Python scripts or applications. This comprehensive guide explains what causes this error and the step-by-step methods for fixing it on any Linux distribution.
What Causes the “python: command not found” Error?
The “command not found” error occurs when the system is unable to locate the executable file for a program you are trying to run. There are two main reasons this happens with the python command:
1. Python is not installed
Python does not come pre-installed on Linux. The python command allows running the Python interpreter or scripts. If Python is not installed, bash cannot find the python executable file to launch it.
2. Python executable path not in $PATH
Even if Python is installed, the directory containing the python executable must be present in the $PATH system variable. $PATH indicates directories search for commands executed. If missing, the python command is unrecognized.
Below we look at solutions to handle both these scenarios – installing Python and updating $PATH to resolve the error for good.
Method 1 – Install Python
If Python is not already installed, you need to install it first before being able to run the python or other Python commands.
Step 1 – Check Python Version Installed
First check if you already have Python but it’s still not working. Type:
python3 --version
If this shows a Python version (e.g. Python 3.8.2), then Python is installed but the executable path requires updating – jump to method 2.
If it returns “command not found”, Python is missing completely.
Step 2 – Install Python
Next install Python using your Linux distribution’s package manager:
Debian/Ubuntu
sudo apt update
sudo apt install python3
Fedora
sudo dnf check-update
sudo dnf install python3
Arch Linux
sudo pacman -Syu
sudo pacman -S python
openSUSE
sudo zypper refresh
sudo zypper install python3
This will install the latest Python 3 version and all required dependencies.
Step 3 – Confirm Python Version
Check it installed correctly:
python3 --version
The Python version will now be displayed.
With Python installed, try running the python command again – this should work directly unless $PATH needs fixing as well.
Method 2 – Add Python to $PATH
If Python is installed but bash still can’t find the command, the next step is adding the Python executable path to the $PATH environment variable.
On Linux, Python 3 installs to /usr/bin/python3 – but $PATH may not contain this path.
Here is how to add it:
Step 1 – Find Python Executable Path
Find where python3 is installed by running:
which python3
This prints the full path to the Python 3 executable file.
E.g. /usr/bin/python3
Step 2 – Get Current $PATH value
Check your current $PATH variable value:
echo $PATH
This lists all paths separated by a colon that are searched when you execute a command.
A typical $PATH is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/usr/bin/python3 needs to be included in $PATH for the python command to work.
Step 3 – Add Python Path to $PATH
You can modify $PATH just for your current session:
export PATH="$PATH:/usr/bin/python3"
However this change is lost when you close the terminal.
To make it permanent, add the path to your shell profile config file:
Bash – Add to ~/.bashrc:
export PATH="$PATH:/usr/bin/python3"
Zsh – Add to ~/.zshrc:
export PATH="$PATH:/usr/bin/python3"
Now source the file to update the current shell:
source ~/.bashrc
OR
source ~/.zshrc
Python’s executable location is now permanently added to $PATH
Step 4 – Confirm with python Command
Finally, confirm Python can now be launched correctly:
python --version
The command should run successfully – python: command not found error resolved!
With this $PATH modification, all Python commands like python, pip, python3 etc will be accessible in Bash.
Method 3 – Create a Soft Link
An alternative to updating $PATH is to create a symbolic soft link to the python3 executable in a directory already in $PATH, like /usr/local/bin.
Step 1 – Create Soft Link
Run this command to symlink python3:
sudo ln -s /usr/bin/python3 /usr/local/bin/python
This creates a virtual python soft link pointing to the real python3 executable.
Step 2 – Run Python Command
Now test running Python via the link:
python --version
With the link in /usr/local/bin present in $PATH, the python command maps to python3 and works normally.
This avoids modifying $PATH but achieves the same effect. Generally updating $PATH directly is the cleaner approach.
Troubleshooting Python Command Not Found Issues
If you still get “python: command not found” after the above steps, a few things to check:
- Make sure Python is fully installed/updated before trying to run commands.
- After adding python path to $PATH, source the shell config file (.bashrc/.zshrc) to update it.
- Look for typos or errors in path added to $PATH – should match “which python3” output exactly.
- If using a soft link, ensure /usr/local/bin is present in $PATH, otherwise link is not resolved.
- Check for spaces or special characters in any added paths that could break commands.
- On Debian/Ubuntu make sure both python and python3 packages are installed for 2.7 and 3.x commands.
- Confirm exact case of the command – python vs Python vs PYTHON makes a difference.
- Try closing terminal and opening a new shell session for $PATH changes to take effect.
- Restarting the system can also flush out any inconsistencies causing issues.
Following this troubleshooting guide helps narrow down the cause and returns the python command up and running on any Linux distribution.
Conclusion
The “python: command not found” error occurs when the Linux shell is unable to locate the Python executable file to launch the command. Installing Python if missing and adding its path to the $PATH system variable are the key solutions discussed in detail.
Here is a summary of the main steps to permanently fix this error:
- Check if Python is installed using the python3 –version command. If missing, install it through the OS package manager.
- Find the path to the Python 3 executable returned by the which python3 command – usually /usr/bin/python3.
- Add this /usr/bin/python3 path either temporarily or permanently to the $PATH variable in bash shell config files like ~/.bashrc.
- Alternatively, create a soft symbolic link to python3 in a standard $PATH directory like /usr/local/bin.
- Reload the shell or open a new terminal for $PATH changes to take effect.
- Run python –version to confirm the command now launches Python successfully.
With Python accessible, you are ready to start running Python scripts and applications smoothly from the Linux terminal and integrate it into various workflows. Troubleshooting any further issues is also easier when the fundamentals of $PATH resolution are understood.
Greetings! I am Ahmad Raza, and I bring over 10 years of experience in the fascinating realm of operating systems. As an expert in this field, I am passionate about unraveling the complexities of Windows and Linux systems. Through WindowsCage.com, I aim to share my knowledge and practical solutions to various operating system issues. From essential command-line commands to advanced server management, my goal is to empower readers to navigate the digital landscape with confidence.
Join me on this exciting journey of exploration and learning at WindowsCage.com. Together, let’s conquer the challenges of operating systems and unlock their true potential.