ModuleNotFoundError: No module named 'x'
ModuleNotFoundError: No module named 'x' in Python means python looked for the module you imported and could not find it. Usually the package is not installed, it is installed for a different Python, or the import name is spelled differently from the package name.
How do I fix ModuleNotFoundError: No module named 'x'?
ModuleNotFoundError: No module named 'x' means Python could not find the module in your import line. Most often the package is not installed for the Python that runs your code. Install it with python -m pip install name, using that same Python. Some packages import under another name, such as cv2 for opencv-python.
- 1. Read the quoted nameCheck the spelling and capital letters of the name in quotes. That is exactly what Python searched for.
- 2. Install for the right PythonRun print(sys.executable), then install with that interpreter: python -m pip install name. Activate your virtual environment first.
- 3. Check the import nameThe pip name and import name can differ: beautifulsoup4 imports as bs4. For your own files, check which folder they are in.
What does ModuleNotFoundError: No module named 'x' mean?
When Python runs import requests, it searches a list of folders for something called requests. If no folder has it, you get this error. The name in quotes is exactly what Python searched for. Note that the name you install with pip is not always the name you import: you install opencv-python but import cv2, and you install beautifulsoup4 but import bs4.
Why does ModuleNotFoundError: No module named 'x' happen?
1. The package is not installed
Only the standard library (math, random, json and so on) comes with Python. Everything else must be installed first.
Breaks
import requests # never installedWorks
# in the terminal, not inside Python:
# python -m pip install requests
import requests2. It is installed for a different Python
Many computers have two or more Pythons. pip may install into one while your editor runs another. Using python -m pip installs into the same Python that runs your code.
Breaks
# terminal
pip install requests
# editor still says: No module named 'requests'Works
# terminal: use the same python your editor uses
python -m pip install requests
# check which one runs your code:
import sys
print(sys.executable)3. The import name is different from the pip name
Some packages are installed under one name and imported under another.
Breaks
import opencv # pip name is opencv-python
import beautifulsoup4Works
import cv2 # pip install opencv-python
from bs4 import BeautifulSoup # pip install beautifulsoup44. Your own file is in another folder
import helpers only finds helpers.py if it sits in the same folder as the script you run (or in a folder Python already searches).
Breaks
# project/main.py
import helpers # but the file is project/utils/helpers.pyWorks
# project/main.py
from utils import helpersHow to find it in your own code
Checklist: 1) Read the name in quotes and check its spelling and case. 2) Run print(sys.executable) to see which Python runs your code, then install with that Python: python -m pip install name. 3) Search the package's page for its import name if it differs from the pip name. 4) For your own files, check the folder layout. 5) If you use a virtual environment, make sure it is activated in the terminal and selected in your editor.
Practise scope and closure bugs
None of the challenges raises this exact error yet. These practise the same kind of bug.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.