If you have updated MacOS Monterey to version 12.5+ then you can’t use python
in commands to refer your Python program. This is removed from Monterey 12.5+. In order to work with it, simply replace all python
reference from commands to python3
. Otherwise you will get Error zsh: Command not found: Python.
Why this happened?
The error suddenly started raising because Apple dropped the support of Python2 from MacOs Monterey 12.5+. xCode requires that you need to have version greater than 12.5. So, you need to update your OS. But update will break the Python.
Although Python2 is removed but Python3 is supported so you can still use that.
Solutions with Code Example
There are 2 solutions to this problem –
- Use
Python3
instead ofPython2
- Create python alias pointing to Python3 in zsh
- Install
Python2
usingpyenv
Let’s look at both these solutions separately.
Using Python3 instead of Python2
Referencing a command with python will try to invoke Python2 in zsh. But since Python2 is not available then we can reference out commands with python3. Check this code –
python myPythonFile.py
This code will raise zsh: Command not found: Python error. To resolve it, you need to run your file with python3 as shown in below code –
python3 myPythonFile.py
Create python alias pointing to Python3 in zsh
Everything will be sorted if you create an alias of python pointing to Python3. What this will do is instead of using Python2 for python reference, it will use Python3.
To create the alias, use this code –
echo "alias python=/usr/bin/python3" >> ~/.zshrc
Now if I run my python file using python command –
python myPythonFile.py # Will not replace as - # python2 myPythonFile.py ❌ # Will replace as - # python3 myPythonFile.py ✔️
It will run without issues.
Install Python2
using pyenv
Although Python2 is not provided by MacOS, you can still install it using pyenv
. Here are the steps –
1. Install pyenv
using brew –
brew install pyenv
2. Check the list of all the versions of python available on pyenv –
pyenv install --list
3. Install a version suitable for your use –
pyenv install 2.7.18
4. Set this version as global version on your system –
pyenv global 2.7.18
5. Open ~/.zprofile
or ~/.bash_profile
or ~/.zshrc
and add the below code there –
eval "$(pyenv init --path)"
6. Restart the terminal and try working again.
Conclusion
In this article we saw the reason of why MacOs Monterey is raising “command not found” error for python. Also, we learned about multiple solutions to solve this issue. After following the steps, you will be able to resolve it completely.