How to Fix ‘zsh: command not found: python’ Error

Table of Contents

If you encounter the error message 'zsh: command not found: python' while trying to execute Python commands in your Terminal using the zsh shell, it indicates that the system cannot locate the Python executable. This error commonly occurs when Python is not properly installed or not added to the system’s PATH environment variable. In this article, we will explore different methods to fix the 'zsh: command not found: python' error in zsh.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  1. A Mac or Linux system running zsh as the default shell.
  2. Python installed on your system.

Method 1: Verify Python Installation

The first step is to verify that Python is installed on your system and accessible. Open a Terminal and execute the following command:

python --version

If Python is installed, it will display the installed version number. If the command is not recognized, proceed to the next method to install Python.

Method 2: Install Python

If Python is not installed on your system, follow these steps to install it:

  1. Open a Terminal.
  2. For Mac users, you can install Python using Homebrew. Run the following command:
brew install python
  1. For Linux users, execute the appropriate package manager command. For example, on Ubuntu, run:
sudo apt update
sudo apt install python3
  1. After installation, verify Python by running python --version again. It should display the installed version.

Method 3: Update PATH Environment Variable

If Python is installed but not added to the system’s PATH environment variable, follow these steps to update it:

  1. Open your Terminal and execute the following command to open the zsh configuration file:
nano ~/.zshrc
  1. Within the file, locate the line that starts with export PATH=.
  2. Append the Python executable path to the existing PATH variable. The path depends on your Python installation. For example, if you installed Python using Homebrew on a Mac, the path should be:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
  1. Save the changes by pressing Ctrl + X, followed by Y, and then Enter.
  2. To apply the changes, execute the following command:
source ~/.zshrc
  1. Test Python by running python --version. It should display the installed version without any errors.

Method 4: Use an Alias

If you prefer not to modify the PATH variable, you can create an alias to access Python. Here’s how:

  1. Open your Terminal and execute the following command to open the zsh configuration file:
nano ~/.zshrc
  1. Add the following line to create an alias:
alias python='/usr/bin/python'
  1. Save the changes by pressing Ctrl + X, followed by Y, and then Enter.
  2. To apply the changes, execute the following command:
source ~/.zshrc
  1. Test Python by running python --version. It should display the installed version without any errors.

Conclusion

By following the methods outlined in this article, you can fix the 'zsh: command not found: python' error in zsh. Whether you need to install Python, update the PATH environment variable, or create an alias, these steps will ensure that Python is recognized and accessible within the zsh shell. Enjoy executing Python commands seamlessly in your Terminal without encountering any command-not-found errors.

Command PATH Security in Go

Command PATH Security in Go

In the realm of software development, security is paramount. Whether you’re building a small utility or a large-scale application, ensuring that your code is robust

Read More »
Undefined vs Null in JavaScript

Undefined vs Null in JavaScript

JavaScript, as a dynamically-typed language, provides two distinct primitive values to represent the absence of a meaningful value: undefined and null. Although they might seem

Read More »