Python

audience developer
level all
topic Python
subtopic Installation

Command not found: python

How to fix command not found: python on Mac. For zsh or bash. You'll see this error when Python is not installed or the Mac $PATH is not set correctly. How to fix the Mac PATH for Python.

New users of Python on macOS often encounter the error zsh: command not found: python. This error occurs when python is not a shell command. The solution is to correctly install Python and set the Mac PATH. But first you need to think about why you are trying to run Python because the solution depends on whether you are running a Python program or developing a Python project.

This is the error you'll see:

$ python ...
zsh: command not found: python

You'll see zsh: command not found: python because you are trying to run the Python interpreter in the terminal. The error message shows that the Zsh shell ("the command line interpreter") cannot find the Python command.

If you see an error that begins with bash... you are using the Bash shell.

$ python ...
bash: command not found: python

If you are using the Bash shell, you likely have an older version of the macOS. Check macOS and update macOS and switch to the Zsh shell.

Before you get started

You'll need a terminal application to fix the error. Apple includes the Mac terminal but I prefer Warp Terminal. Warp is an easy-to-use terminal application, with AI assistance to help you learn and remember terminal commands. Download Warp Terminal now; it's FREE and worth a try.

Are you programming in Python or just running a utility?

Before you try to fix the error zsh: command not found: python, think about why you are trying to use Python.

People use Python on a Mac for two different reasons: running a program or writing code. If you are running a program such as a script or utility, it's okay to use the system Python installed with Xcode Command Line Tools or Homebrew, though it's better to install programs using Pipx. If you are a developer, you need to install Python and set up a development environment.

The solution to the error zsh: command not found: python will be different depending on whether you are developing a Python project or just running a program.

For Python programs and utilities, use Pipx

When you see zsh: command not found: python, you may be trying to run a Python utility or script. You should use Pipx, a tool to install and run Python applications. See the article:

When you install Pipx with Homebrew, Python will be installed for you. This is the easiest way to fix the error zsh: command not found: python if you want to run a Python program such as a utlity or script.

Instead of using Pipx with pipx install, READMEs and tutorials often say to install Python utilities with pip install. This is not recommended because it installs a program globally and can cause conflicts with other programs. Often, package installation fails with error: externally-managed-environment (see error: externally-managed-environment). Instead, use Pipx to install Python applications in isolated environments. With Pipx, you can use the system Python without interfering with system software.

For programming in Python, use Pyenv or Rye

If you are starting a programming project with Python, you need to install Python and set up a development environment.

There are several ways to install Python on a Mac. You can install Python with Homebrew or use a Python version manager such as Pyenv or Rye. Either:

If you're developing Python software projects, ask yourself if you're just going to develop one project or if you need to work on multiple Python projects. For a single project you can use brew python to install Python with Homebrew but you won't be able to easily switch between Python versions. For multiple projects, switching among Python versions, I recommend to install Python with Rye, an all-in-one tool for managing Python projects. Alternatively, you can install Pyenv to manage Python versions but it's older and more cumbersome than Rye.

Instructions for installing Python will explain how to set the $PATH in the .zprofile or .zshrc configuration file. This is necessary for the shell to find the Python command.

Fix error 'command not found: python'

If you've considered whether you are running a Python program or developing a Python project, you can now fix the error zsh: command not found: python. If you just need to run a Python program, follow the guide Install Pipx and Python will be installed for you. If you are developing a Python project and writing code, the solution will depend on whether Python is installed, where it is installed, and whether the Mac $PATH is set correctly.

Possible reasons for this error:

  1. You need to restart your Terminal ("reset the shell").
  2. A system Python is installed but not in the $PATH.
  3. A newer Python version is installed but not in the $PATH.
  4. Python is not installed.

Here is what went wrong:

  • You may be following instructions that assume you have installed Python previously but you didn't.
  • Maybe you installed Python previously but it is now missing.
  • You need to restart the Terminal after installing Python and setting the $PATH.
  • Most likely: $PATH is not set in a ~/.zprofile or ~/.zshrc configuration file. Without a correct $PATH, the shell cannot find the python command.

You can read the guide Mac PATH for more information about the $PATH setting.

Restart your Terminal

Just to be sure that you don't have the simplest reason for an error, quit the Terminal application and launch the Terminal again. Then try:

$ python --version

It's an obvious solution but worth trying. A restart should display the Python version number.

If you still see zsh: command not found: python, read on.

Is the system Python installed?

Try python3 --version and which -a python3 to check if Python was installed with Xcode Command Line Tools.

$ python3 --version
Python 3.9.6
$ which -a python3
/usr/bin/python3

If you have Python 3.9.6 installed at /usr/bin/python3, you have the system Python installed by Xcode Command Line Tools. Don't use the system Python if you are programming in Python. Instead, install another version of Python (described below) and set the Mac PATH.

If you just want to run Python scripts or command-line utilities, you can use the system Python. However, I recommend to install Pipx with Homebrew if you want to run a script or command-line utility. The Homebrew Python is newer and it's easy to install.

If you want to use the system Python (which I don't recommend), you can alias the python command to python3. Add the following line to your .zshrc file. See the article alias python3 to python for more information:

alias python='python3'

For this change to take effect, you'll either need to restart your terminal or reload your .zshrc file by entering source ~/.zshrc.

Is Python already installed?

Python can be installed in various ways: with the official installer, Homebrew, or a Python version manager such as Pyenv or Rye. If you believe you have Python installed and you are getting the error zsh: command not found: python, you likely have a problem with the $PATH setting.

You will need to check the $PATH in the .zprofile or .zshrc configuration file. By convention and design, the ~/.zprofile file is used for setting the $PATH (see .zshrc or .zprofile for an explanation). If you don't have a .zprofile file, check the .zshrc file.

Fix the Mac PATH for Python

Before you can fix the Mac $PATH setting, you need to determine how Python was installed and where it is located.

Python installed with the official installer

Maybe Python was installed with the Python official installer.

The installer package from the Python website adds files to /usr/local/bin, modifies your Mac PATH, and installs the latest Python version in the macOS /Library/Frameworks folder.

Check the contents of the Python installation in the macOS Frameworks directory:

$ ls -l /Library/Frameworks/Python.framework

This command will list any the versions of Python installed with the official installer. If Python is not there, it was not installed with the official installer. If it's there, the official installer should have set the Mac PATH correctly and set symbolic links in /usr/local/bin to the Python executable. If it was not set up correctly, it's best to reinstall Python with Homebrew, Pyenv, or Rye (see update Python).

Though it's the official Python website installer, most Python developers avoid using it because it clutters a Mac in ways that are difficult to manage.

Python installed with Homebrew

You can check if python was installed with Homebrew. It will be listed among the Homebrew-installed packages:

$ brew list | grep python
[email protected]

This will show a Python version if it is installed via Homebrew. If you see zsh: command not found: brew, you don't have Homebrew. You may want to install Homebrew for your projects. If you don't want Homebrew, you can install Python with Rye, the all-in-one tool for managing Python projects.

If Homebrew shows Python is installed, you can get more information about the Python version, including its installation path:

$ brew info python
==> [email protected]: stable 3.12.3 (bottled)
.
.
.

This command will give you a summary that includes the version of Python installed via Homebrew and the path to the executable.

Installing Python with brew install python doesn't set the Mac PATH. You can add the Homebrew Python executable path directly to the Mac PATH in the .zprofile file. However, Homebrew recommends modifying the ~/.zprofile file by adding the following line as the last line in the ~/.zprofile file:

export PATH="$(brew --prefix python)/libexec/bin:$PATH"

Changes to the ~/.zprofile file will not take effect in the Terminal until you've quit and restarted the terminal.

Your Mac PATH should contain the Python executable directory:

$ echo $PATH
/opt/homebrew/opt/[email protected]/libexec/bin:...

Entering python --version should now display the Python version number. This solves the error zsh: command not found: python.

Python installed with Pyenv

Pyenv is a popular Python version manager for installing multiple Python versions and switching between them. It is typically installed with Homebrew. If you have Homebrew installed, you can check if Pyenv is installed:

$ brew list | grep pyenv
pyenv

Note that I recommend using Rye instead of Pyenv as a Python version manager. Rye is an all-in-one tool that provides a streamlined solution for Python development.

You can verify that Pyenv is installed by running:

$ pyenv --version
pyenv 2.4.0

If Pyenv is installed, you can list all Python versions installed on your system through Pyenv:

$ pyenv versions
* system (set by /Users/daniel/.pyenv/version)
3.12.2

This command will display a list of all Python versions that have been installed with Pyenv. The currently active Python version (the one being used in your terminal session) is indicated by an asterisk (*). You'll see only system if you haven't installed any Python versions with Pyenv.

Check if a Pyenv global version is set:

$ pyenv global
system

If Pyenv shows a response "system," make sure you've installed a Python version with Pyenv and set it as the global version. Pyenv doesn't set a default Python version the first time you install Python. Instead, it defaults to a system Python (which doesn't exist after macOS 12.3 unless Xcode Command Line Tools is installed).

You can set a Python version as the global version with:

$ pyenv global 3.12

If you've installed a Python version with Pyenv, seen it listed by pyenv versions, and set it as the global version, you'll still need to set the Mac PATH to eliminate the error zsh: command not found: python.

Edit your ~/.zprofile file to add Python versions installed by Pyenv to the Mac PATH. Add the following lines:

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Changes to the ~/.zprofile file will not take effect in the Terminal until you've quit and restarted the terminal.

Entering python --version should now display the Python version number. This solves the error zsh: command not found: python.

Python installed with Rye

If Python was installed with Rye, you can list installed Python versions:

$ rye --version
rye 0.32.0
$ rye toolchain list
[email protected] (/Users/daniel/.rye/py/[email protected]/bin/python3)

Your Mac PATH should contain the Rye shims directory:

$ echo $PATH
/Users/daniel/.rye/shims:...

If you need to fix the Mac PATH, don't add the Python executable path directly. Instead, add the Rye shims directory to the Mac PATH using the Rye ~/.rye/env script. Add this command as the last line of your ~/.zprofile file to configure the Z shell for Rye.

source "$HOME/.rye/env"

When your terminal session starts, Z shell will run the ~/.rye/env script to set shims to intercept and redirect any Python commands. You'll need double quotes because the command contains spaces or special characters. Rye adds the shims to your $PATH so that running the command python or python3 will run a Rye-installed Python version.

Changes to the ~/.zprofile file will not take effect in the Terminal until you've quit and restarted the terminal.

Entering python --version should now display the Python version number. This solves the error zsh: command not found: python.

What's next

My mac.install.guide is a trusted source of installation guides for professional developers. Take a look at the Mac Install Guide home page for tips and trends and see what to install next.