DEV Community

Cover image for How to fix 'zsh: command not found: python'
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

How to fix 'zsh: command not found: python'

When trying to run python from terminal, it is possible to run into the following issue:

zsh: command not found: python
Enter fullscreen mode Exit fullscreen mode

This can happen on any system, but does occur slightly more commonly on MacOS since they removed native python support in MacOS 12.3. Fortunately this issue is easy to fix.

Step 1: make sure Python is installed

The first thing you should check is that python is installed. You can install python from terminal if you have brew installed by simply typing:

brew install python
Enter fullscreen mode Exit fullscreen mode

You can also download the executable directly from the Python website to install it. Once python is installed, try running python from terminal again. If you still face the same issue, move to step 2.

Step 2: add python to zsh

The next step is to add python to zsh so that it will run upon typing the python command. You can do this by running the following in terminal:

echo "alias python=/usr/bin/python3" >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

This will configure your zsh profile to run /usr/bin/python3 when python is run. If you are still facing issues, ensure that python=$ where the $ sign should equal the path python is installed on.

Step 3: restart terminal

Now that you've done that, simply restart your terminal. When you open it again, your python command should work successfully.

Top comments (11)

Collapse
 
sixman9 profile image
Richard Joseph • Edited

I'm on an M1 Mac, the following worked for me as Step 2 (May 2023)

echo 'export PATH="$(brew --prefix)/opt/python@3/libexec/bin:$PATH"' >> ~/.zshrc

Found that at 'How do I use brew installed Python as the default Python?' on Stackoverflow.

Collapse
 
ahmad-ali14 profile image
Ahmad Ali

Thanks, this is super helpful

Collapse
 
chanzain profile image
Chan Zain

Thanks

Collapse
 
udit_rautela_ca940009ea3a profile image
udit rautela • Edited

thanks @smpnjn

Collapse
 
johnmalib profile image
johnmalib

This error occurs because the system-provided Python 2 installation has been removed from Apple's macOS as of macOS 11 Big Sur.

When Apple removed the system-provided Python 2 installation, it impacted users who were relying on it for their Python development environment. As a result, they may have encountered the error message zsh: command not found: python when trying to run the python command.

To resolve this issue, you need to install a newer version of Python (such as Python 3 ) or use a version manager like Homebrew or pyenv to manage your Python installations. These tools allow you to install multiple versions of Python and switch between them as needed.

Collapse
 
vijaykhatri96 profile image
Vijay Singh Khatri

@smpnjn You can also add this post in your reference list that can be beneficial fo user who want to understand the concept of brew command not found error.

Collapse
 
padunk profile image
Abraham A. Agung

this path work for me /usr/local/bin/python3

Collapse
 
reecestart profile image
Reece

Thanks

Collapse
 
vinodsharma profile image
Vinod Sharma

Thank you for your help

Collapse
 
swlkr_11 profile image
Sam Walker

THANK YOU SO MUCH

Collapse
 
__e22830f76744 profile image
Khaustoff

Thanks