You ran your Python code and got “ModuleNotFoundError: No module named ‘…'” — now what?
It might look alarming, but don’t worry. This is one of the most common errors that almost every Python beginner encounters.
In this article, we’ll walk through why ModuleNotFoundError happens and how to fix it. By the end, you’ll know exactly how to handle this error whenever it pops up.
目次
Why ModuleNotFoundError happens
ModuleNotFoundError means “Python can’t find the module (a bundle of code) you’re trying to use.” The main causes are:
- The library isn’t installed: You haven’t installed the package you’re trying to import yet
- Typo in the module name: There’s a spelling mistake in your import statement
- Installed in a different Python environment: You might have multiple Python installations, and the package was installed in the wrong one
Solution 1: Install the missing library (most common fix)
The most common cause is simply not having the library installed yet.
1. Check the error message for the module name
# Example error message
ModuleNotFoundError: No module named 'requests'
# → "requests" is the library you need to installThe name after “No module named” is what you need to install.
2. Install it with pip
On Windows:
pip install requestsOn Mac:
pip3 install requestsIf you see “Successfully installed”, you’re good.
3. Run your code again to confirm the error is gone
# Run your script again
python your_script.pyIf the error disappears, you’ve solved it.
Solution 2: Check for spelling mistakes
A surprising number of ModuleNotFoundErrors are caused by simple typos.
1. Double-check your import statement
# Common mistakes
import reqeusts # ✗ Typo
import requests # ✓ Correct
import beautifulsoup4 # ✗ Wrong — this is the pip name, not the import name
from bs4 import BeautifulSoup # ✓ CorrectPay special attention: some libraries have different names for pip install and import.
2. Common package name vs. import name differences
# pip install name → import name
# beautifulsoup4 → bs4
# Pillow → PIL
# scikit-learn → sklearn
# python-dotenv → dotenv
# opencv-python → cv2Check the library’s official documentation if you’re unsure about the correct import name.
Solution 3: Make sure you’re using the right Python environment
If you’ve installed a package but still get the error, you may have installed it in a different Python environment.
1. Check which Python you’re currently using
On Windows:
# Show the path of your active Python
where pythonOn Mac:
# Show the path of your active Python
which python32. Install the package for that specific Python
# This ensures the package is installed for the Python you're actually running
python -m pip install requestsUsing python -m pip install ties the installation directly to the Python you’re running, which avoids environment mismatches. If the installation completes successfully, you’re all set.
If nothing works
When the above methods don’t solve the problem, try resetting your environment with a virtual environment (venv).
# Create and use a fresh virtual environment (Windows)
python -m venv myenv
myenv\Scripts\activate
pip install requests
python your_script.py
# On Mac
python3 -m venv myenv
source myenv/bin/activate
pip install requests
python your_script.pyVirtual environments keep your libraries organized and prevent most ModuleNotFoundError issues.
When asking for help online, include these details for faster answers:
- The full error message
- The output of
python --version - The output of
pip list(shows all installed packages)
Summary
- ModuleNotFoundError usually means the library isn’t installed — run
pip installfirst - Watch out for libraries where the pip name and import name are different
- Use
python -m pip installto avoid environment mismatch problems
Related articles:
- pip-install-error.html (How to fix pip install errors)
- python-path-windows.html (How to fix Python PATH on Windows)

















Leave a Reply