Fixing "zsh: command not found: python" on macOS Monterey
Problem
The "zsh: command not found: python" error occurs on macOS Monterey (12.3+) because Apple removed the system-provided Python 2.7 installation. This affects tools and packages that reference the python
command directly, such as the atom-python-run package mentioned in the original question.
This change is intentional by Apple and affects all macOS Monterey users. The system no longer includes any Python runtime by default, which breaks scripts and tools that rely on the python
command.
Solutions
Quick Fix: Create an Alias
The simplest solution is to create an alias in your shell configuration:
echo "alias python=/usr/bin/python3" >> ~/.zshrc
source ~/.zshrc
This tells zsh to redirect any python
command to python3
. After running these commands, verify it works:
python --version
WARNING
This approach may not work for all scenarios, particularly scripts that use absolute paths or have specific Python version requirements.
Recommended Solution: Install Python via Homebrew
For a more robust solution, use Homebrew to install and manage Python:
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Python
brew install python
# Verify installation
python3 --version
After installation, you can create the alias as shown above, or use python3
explicitly in your commands.
Advanced Solution: Use pyenv for Version Management
For developers who need multiple Python versions, pyenv is the ideal solution:
# Install pyenv
brew install pyenv
# Install Python version (example: 3.10.6)
pyenv install 3.10.6
# Set as global default
pyenv global 3.10.6
# Add pyenv to your shell
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
Now the python
command will use your pyenv-managed Python installation:
python --version
Common Scenarios
For Atom Python Runner
If you're using the atom-python-run package, you may need to update its configuration to use python3
instead of python
:
- Open Atom preferences
- Navigate to the atom-python-run package settings
- Change the command from
python
topython3
For Scripts and Automation
For scripts that require the python
command, you have several options:
# Change the first line from:
#!/usr/bin/env python
# To:
#!/usr/bin/env python3
# Create a symbolic link (may require sudo)
sudo ln -s /usr/bin/python3 /usr/local/bin/python
# Add this at the beginning of your shell scripts
alias python=python3
For Python 2.7 Legacy Needs
If you specifically need Python 2.7 (not recommended for new projects):
# Install Python 2.7 via pyenv
pyenv install 2.7.18
pyenv global 2.7.18
# Or install via Homebrew (versioned formula)
brew install python@2
Verification and Troubleshooting
After applying any solution, verify it works:
# Check Python version
python --version
# Check installation path
which python
# Test simple Python code
python -c "print('Hello, World!')"
If you encounter issues:
Check your PATH: Ensure Python's directory is in your PATH
bashecho $PATH
Restart your terminal: Some changes require a new terminal session
Check shell configuration files: Ensure changes are in the correct file (~/.zshrc for zsh)
Best Practices
- Always use
python3
explicitly in new scripts and documentation - Use virtual environments for project-specific dependencies:bash
python3 -m venv myenv source myenv/bin/activate
- Consider using pyenv if you work with multiple Python versions
- Keep your Python installation updated with security patches
TIP
Apple's removal of Python 2.7 is part of their effort to reduce pre-installed legacy software. This change encourages using current, supported Python versions and proper dependency management.
The solutions provided above will resolve the "zsh: command not found: python" error while establishing a more maintainable Python development environment on macOS.