DEV Community

Cover image for ModuleNotFoundError After pip install? Here’s the Fix
Yunus Emre Güzel
Yunus Emre Güzel

Posted on Originally published at blog.emreguzel.ca

ModuleNotFoundError After pip install? Here’s the Fix

You run pip install requests. It says Successfully installed. You run your script, and Python answers ModuleNotFoundError: No module named 'requests'. A ModuleNotFoundError after pip install almost always means one thing: your computer has more than one Python, and pip put the package into a different one.

ModuleNotFoundError after pip install: plain pip install compared with python -m pip install

The Problem: ModuleNotFoundError After pip install

It looks like this in the terminal:

> pip install requests
Successfully installed requests-2.32.3

> python app.py
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
Enter fullscreen mode Exit fullscreen mode

The install worked. The import failed. Both are telling the truth.

Why It Happens

One PC can easily have three or four Pythons: one from python.org, one from the Microsoft Store, one inside a virtual environment, and one your editor picked for you. Each has its own folder of installed packages, and they don’t share.

pip is a separate command, and it belongs to just one of those Pythons. If pip belongs to Python A and python app.py starts Python B, the package is installed and Python B still can’t see it.

Step 1: See Which Python Runs Your Script

Add these two lines to the top of your script and run it:

import sys
print(sys.executable)
Enter fullscreen mode Exit fullscreen mode

That prints the full path of the Python running your code. Now ask pip which Python it belongs to:

pip --version
Enter fullscreen mode Exit fullscreen mode

The answer ends with a path and a version in brackets. If the two paths point to different folders, you’ve found the bug.

The Fix for ModuleNotFoundError After pip install

Stop calling pip by itself. Run it through the Python you actually use:

python -m pip install requests
Enter fullscreen mode Exit fullscreen mode

python -m pip means “run the pip that belongs to this Python”. The package can only land in the right place. On Windows, if you start scripts with the py launcher, use py -m pip install requests.

Other Causes Worth Checking

  • Your editor uses a different Python. In VS Code, press Ctrl+Shift+P, choose “Python: Select Interpreter” and pick the one sys.executable printed.

  • The virtual environment isn’t active. A package installed inside a venv exists only there. Activate it first: .venv\Scripts\activate on Windows, source .venv/bin/activate on Mac and Linux.

  • The install name and the import name are different. pip install pillow but import PIL. pip install pyserial but import serial. pip install opencv-python but import cv2.

  • One of your own files has the module’s name. A file called requests.py in your project folder gets imported instead of the real package. Rename it.

So when you see a ModuleNotFoundError after pip install, check which Python you’re in before you reinstall anything. Python’s own guide to installing Python modules recommends the same python -m pip habit.

New to modules? Start with my guide to Python modules. If you’re just beginning, Python for beginners comes first.

Which module refused to import for you? Tell me in the comments, with what sys.executable printed, and I’ll help you sort it out.


Originally published on my blog. I post new C, Python and AI-agent guides there regularly.

Top comments (0)