You ran pip install, saw “Successfully installed,” and your Python script still raises ModuleNotFoundError. Start with the name Python says is missing. Then check which Python runs your script and use that same interpreter to check the installation. If you already have the right environment, look up whether the package’s installation name differs from its import name.
1. Copy the missing import name from the traceback
Run your script again and read the last line of the traceback. For example, suppose app.py contains import requests:
Traceback (most recent call last):
File "/home/you/project/app.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
The missing name is requests. Copy the text between the quotes, preserving capitalization and any dots. The lines above the error tell you which file tried to import it. Sometimes the failing import is inside a library your script uses. If you wrote import example_library and the traceback ends with No module named 'requests', investigate requests first.
A dotted name gives you more detail. In No module named 'PIL.Image', PIL is the top-level import and Image is the part Python could not find. Keep the full name for troubleshooting, and use the top-level name when looking up which package provides it.
2. Check which Python runs your script
A computer can have several Python installations and virtual environments. The pip command in your terminal can install into one environment while your editor runs the script in another.
Temporarily put these two lines near the top of your script, before the failing import:
import sys
print(sys.executable)
Run the script the same way you normally do. If you usually click Run in your editor, click Run again. The printed path might look like this:
/home/you/project/.venv/bin/python
Now check where the terminal’s standalone pip command is running:
pip --version
Its output includes pip’s location and the Python version it uses. If the location is outside your .venv, you may have installed the package into a different environment. A virtual environment created with --system-site-packages can also load pip from outside .venv. Two environments can both report the same Python version, so treat pip’s location as a clue.
To make sure the interpreter that runs your script also runs pip, copy the path printed by sys.executable and use it with -m pip:
"/home/you/project/.venv/bin/python" -m pip --version
"/home/you/project/.venv/bin/python" -m pip show requests
In PowerShell, use & to run a quoted executable path:
& "C:\Users\you\project\.venv\Scripts\python.exe" -m pip --version
& "C:\Users\you\project\.venv\Scripts\python.exe" -m pip show requests
Replace the example path with your own. If pip show prints package details, including a Location field, that interpreter’s pip can see the installation. If it says Package(s) not found and you know requests is the correct installation name, install it with the same interpreter:
"/home/you/project/.venv/bin/python" -m pip install requests
If your usual launch command is python app.py, you can use python -m pip install requests in the same terminal. If you run python3 app.py, use python3 -m pip install requests. The -m pip form runs pip through the Python command you chose.
3. Look up the installation name when it differs from the import
Python’s import statement looks for a module or import package. Pip installs a distribution, which is the project published under an installation name. The names often match, as with requests, but they can differ.
| If your code imports | Distribution to install or check |
|---|---|
requests |
requests |
PIL |
Pillow |
yaml |
PyYAML |
bs4 |
beautifulsoup4 |
Check the project’s installation instructions or the library’s official documentation for the distribution name before installing anything based on the traceback. For example, when from PIL import Image raises No module named 'PIL', check for Pillow:
python -m pip show Pillow
Run python -m pip install Pillow only if pip show reports that Pillow is missing. Then test the import:
python -c "from PIL import Image; print(Image.new('RGB', (2, 2)).size)"
The last command should print (2, 2). These commands assume python runs your script. Substitute python3 or the executable path you found if needed.
The missing name can also belong to your own project. If import helpers is supposed to load a helpers.py file from your codebase, check the file’s name and the project’s documented launch command. Installing a distribution named helpers could install an unrelated distribution if one exists under that name.
4. Test the import, then rerun your script
Test the exact import that failed using the same interpreter. For the requests example, run this from your project directory:
python -c "import requests; print(requests.__file__)"
The python -c command tests the import with the chosen interpreter and, by default, puts the current directory first on its import search path. Running a script by file path puts the script’s directory first by default, so the test and your usual launch can resolve imports differently even when sys.executable matches. Python’s -P and -I flags and the PYTHONSAFEPATH environment variable can suppress that directory entry.
If it succeeds, the printed path shows which copy Python loaded. Rerun your script using your usual launch method. If the terminal test works but your editor still fails, compare sys.executable in both places. If the paths differ, select the working interpreter in your editor. If the paths match, compare the launch method and sys.path in both contexts.
If pip show finds the distribution but the import still fails, use the traceback to narrow it down:
- A different name is missing. The library may have reached an optional dependency. Check its installation instructions for the feature you’re using.
-
Only a submodule is missing. Run
pip showfor the distribution and compare its installed version with the documentation or tutorial you’re following. The submodule may have moved or been introduced in another version. -
The traceback points to a file in your project with the library’s name. A file such as
requests.pycan take precedence over the installed library. Rename the local file if it is unrelated to the library, then rerun the script.
Summary
Copy the exact missing name from the last line of the traceback. Find your script’s interpreter with sys.executable and use that interpreter’s -m pip to check or install the distribution. Confirm the install name in the documentation, then test the original import before rerunning your script.
Want a shortcut? Python Package Release Rehearsal and Handoff Kit — a checklist kit for handing off and rehearsing a PyPI release ($12).
Top comments (0)