NameError: name 'pip' is not defined in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# NameError: name 'pip' is not defined in Python [Solved]

The "NameError: name 'pip' is not defined" occurs for multiple reasons:

  1. Trying to use a pip command in an interactive shell session.
  2. Trying to use a pip command inside of a Python module.

nameerror name pip is not installed

# Exit the interactive shell session before using pip

If you entered an interactive shell session by typing "python", "python3" or "py" in your terminal, use the exit(), exit or press CTRL + D to quit the shell session.

shell
exit()

exit-interactive-session

Once you exit the shell session, you can issue any pip command.

main.py
pip install requests # ๐Ÿ‘‡๏ธ Or with pip3 pip3 install requests

pip install package

# Trying to run a pip command from inside a Python module

Another common cause of the error is trying to run a pip command from a Python module.

Here is an example of how you would install a module in a Python script.

The following file is named main.py.

main.py
import sys import subprocess python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL )

install module from within script

You can run the file with python main.py, python3 main.py or py main.py to install the requests module.

Make sure to replace requests with the name of the module you're trying to install.

You can also remove the stdout keyword argument if you want to see the output of the pip command when running python main.py.

main.py
# โœ… Shows output of the pip install command import sys import subprocess python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'] )

# Installing a package from your shell using pip

Here is an example of how you would install a package from your shell (e.g. CMD, PowerShell, bash, etc).

shell
pip install requests pip3 install requests # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install requests python3 -m pip install requests # ๐Ÿ‘‡๏ธ Using py alias (Windows) py -m pip install requests # ๐Ÿ‘‡๏ธ If you get a permissions error pip install requests --user

install package from within shell

Make sure to replace requests with the name of the package you're trying to install.

# Upgrading your version of pip

If the error persists, try to run the following command to upgrade pip.

shell
# ๐Ÿ‘‡๏ธ On Linux or macOS python -m ensurepip --upgrade # ๐Ÿ‘‡๏ธ Using python 3 python3 -m ensurepip --upgrade # ๐Ÿ‘‡๏ธ On Windows py -m ensurepip --upgrade

upgrade your pip version

If you get an error that pip is not found or "no module named pip", check out my other article with instructions on how to install and upgrade pip.

# Conclusion

To solve the "NameError: name 'pip' is not defined", make sure:

  1. You aren't trying to use a pip command in an interactive shell session.
  2. You aren't trying to use a pip command directly in a Python module.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev