[SOLVED] zsh: command not found: pip on MacOS


Tips and Tricks

Author: Omer Cakmak
Reviewer: Deepak Prasad

The error message "zsh: command not found: pip" occurs when you try to run the pip command in a zsh (Z shell) environment, but the system is unable to locate the pip executable. This error is a common issue encountered by users working with Python environments on systems where zsh is set as the default shell, such as on macOS or on systems where the user has chosen zsh over other shells like bash.

The most common reasons to get this error can be:

  • Pip is Not Installed: The most straightforward reason is that pip is not installed on your system. This can happen if Python was installed without the option to include pip.
  • Path Issues: Even if pip is installed, the shell may not know where to find it if its location is not included in the PATH environment variable. This can happen if the installation directory of pip is not added to the PATH or if there are errors in the PATH configuration.
  • Virtual Environment Misconfiguration: If you are working within a Python virtual environment and it is not activated correctly, the shell might not find pip because it is looking outside the virtual environment.
  • Python Installation Problems: An improperly installed or corrupted Python setup can also lead to this issue, where pip might not have been installed correctly.

 

1. Install pip Using get-pip.py

If pip is not installed, you can install it using the get-pip.py script. First download the get-pip.py script:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Execute the script:

python3 get-pip.py

This command requires python3 to be already installed otherwise you can follow Install Python Package from Github

 

2. Check and Update the PATH Variable

Ensure that the directory containing pip is included in your PATH environment variable.

Open the Terminal and check the PATH variable

echo $PATH

If the directory containing pip (e.g., /usr/local/bin or /Library/Frameworks/Python.framework/Versions/3.x/bin) is not listed, you need to add it. You can get the path of pip and python using which command:

which pip
which python3

Open your .zshrc file in a text editor:

nano ~/.zshrc

Once you have the paths, you can verify them and then update your .zshrc file accordingly.

Add the following line (adjust the path to match your pip installation):

export PATH="/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.x/bin:$PATH"

Ensure that /usr/local/bin and the Python framework directory you found in the previous steps are included.

Save and exit the text editor (Ctrl+X, then Y to confirm).

Apply the changes to your current session:

source ~/.zshrc

 

3. Reinstall pip Using Homebrew

Homebrew is a popular package manager for macOS. You can use it to install Python and pip.

Install Homebrew (if not already installed):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Python (which includes pip):

brew install python

Verify installation:

python3 --version
pip3 --version

 

4. Virtual Environments and PIP

When using virtual environments in Python, each environment has its own installation of pip and other dependencies. If the virtual environment is not activated, the system-wide pip command may not be available or might not be the correct version.

To activate your virtual environment:

source /path/to/your/virtualenv/bin/activate

Sometimes, you might activate a different virtual environment that doesn’t have pip installed.

deactivate
source /correct/path/to/virtualenv/bin/activate

The virtual environment might be corrupted or improperly set up. Recreate the virtual environment:

deactivate
rm -rf /path/to/your/virtualenv
python3 -m venv /path/to/your/virtualenv
source /path/to/your/virtualenv/bin/activate

Once activated, check pip

pip --version

If pip is not found even after activating the virtual environment, you might need to install it within the environment. Use the ensurepip module to install pip:

python -m ensurepip --upgrade

Alternatively, use the get-pip.py script:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

After reinstalling pip, verify that it is correctly installed and accessible within the virtual environment:

pip --version

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment