How to Fix the zsh command not found python Error

If you’re working on a macOS system and encounter the “zsh: command not found: python” error, it usually means that the Python executable is not installed or not correctly configured in your system’s PATH
. This issue is common, especially after macOS removed the pre-installed Python 2. Here’s how to resolve it:
Step-by-Step Solution
Step 1: Verify Python Installation
First, check if Python is installed on your system. Open your terminal and run:
1 | python3 --version |
If you see the version number, Python is installed, but the python
command might not be correctly linked. If you see an error, proceed to the next step.
Step 2: Install Python
If Python is not installed, you can install it using Homebrew, a package manager for macOS:
Install Homebrew (if not already installed):
1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Python:
1
brew install python
This will install Python 3 and its associated tools, including pip
.
Step 3: Update Your Shell Configuration
After installing Python, you need to ensure that the python
command points to the correct executable. You can do this by creating an alias in your Zsh configuration file.
Open your
~/.zshrc
file in a text editor:1
nano ~/.zshrc
Add the following line to the file:
1
alias python=/usr/bin/python3
Save the file and reload your Zsh configuration:
1
source ~/.zshrc
This will configure your shell to run /usr/bin/python3
when you type python
.
Step 4: Verify the Fix
To ensure the fix worked, run:
1 | python --version |
You should see the version number of Python 3. If you still encounter issues, double-check the path in your ~/.zshrc
file and ensure it matches the location of your Python installation.
Additional Tips
Use
python3
Directly: If you prefer not to create an alias, you can always usepython3
instead ofpython
in your commands.Check Your PATH: Ensure that the directory containing the Python executable is included in your
PATH
environment variable. You can check this by running:1
echo $PATH
If necessary, update your
PATH
in~/.zshrc
by adding:1
export PATH=$PATH:/usr/local/bin
Reinstall Xcode Command-Line Tools: In some cases, corrupted Xcode command-line tools can cause issues. Reinstall them by running:
1
xcode-select --install
This step is especially useful if you encounter persistent errors.
Conclusion
The “zsh: command not found: python” error is a common issue on macOS, but it can be easily resolved by ensuring Python is installed and correctly configured in your shell. By following these steps, you should be able to use the python
command without any issues. Happy coding!