Skip to content

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:

bash
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:

bash
python --version

WARNING

This approach may not work for all scenarios, particularly scripts that use absolute paths or have specific Python version requirements.

For a more robust solution, use Homebrew to install and manage Python:

bash
# 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:

bash
# 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:

bash
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:

  1. Open Atom preferences
  2. Navigate to the atom-python-run package settings
  3. Change the command from python to python3

For Scripts and Automation

For scripts that require the python command, you have several options:

bash
# Change the first line from:
#!/usr/bin/env python

# To:
#!/usr/bin/env python3
bash
# Create a symbolic link (may require sudo)
sudo ln -s /usr/bin/python3 /usr/local/bin/python
bash
# 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):

bash
# 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:

bash
# Check Python version
python --version

# Check installation path
which python

# Test simple Python code
python -c "print('Hello, World!')"

If you encounter issues:

  1. Check your PATH: Ensure Python's directory is in your PATH

    bash
    echo $PATH
  2. Restart your terminal: Some changes require a new terminal session

  3. Check shell configuration files: Ensure changes are in the correct file (~/.zshrc for zsh)

Best Practices

  1. Always use python3 explicitly in new scripts and documentation
  2. Use virtual environments for project-specific dependencies:
    bash
    python3 -m venv myenv
    source myenv/bin/activate
  3. Consider using pyenv if you work with multiple Python versions
  4. 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.