ModuleNotFoundError: No module named
Python couldn't find the module to import: its name is spelled differently, the file isn't in the project, or the package isn't installed here.
Your error
Check these first
- Spelling and capitals.
import math, notmathsorMath. - Your own files.
import helperneeds a file named exactlyhelper.pyin the same project. - Packages. Some packages aren't available in an online editor, and some import under a different name than the one you install.
Causes and fixes
1 Spelling and capitals
Module names are exact and usually all lowercase: math, random, datetime, numpy.
import maths
print(maths.pi)
ModuleNotFoundError: No module named 'maths'
import math
print(math.pi)
3.141592653589793
2 Your own file isn't in the project
When you split a program into files, from helper import greet looks for a file named helper.py next to yours. Add it with the + button beside the file tabs, with exactly that name.
3 A package this editor doesn't have
Python's own modules, like math, random and json, are always there, and so are popular packages such as NumPy, pandas, Matplotlib, scikit-learn and SciPy. Large ones like TensorFlow and PyTorch, and ones that need a desktop or a web server, aren't: run those on your own computer after pip install.
4 The import name isn't the install name
Some packages import under another name: opencv-python is import cv2, scikit-learn is import sklearn, beautifulsoup4 is import bs4, pillow is import PIL and pyyaml is import yaml.
5 A name that isn't in the module
from math import squareroot fails because math has no squareroot. Check the spelling of the name after import (here sqrt). Two of your own files importing each other give the same error: move what they share into a third file.
Still stuck?
- In the traceback,
File "main.py", line 3says where the error happened. In the editor, click that line to go to it. - Go over the basics: Modules and Imports
- Other errors: AttributeError, FileNotFoundError, IndentationError, IndexError, KeyError, NameError, RecursionError, SyntaxError, TypeError, UnboundLocalError, ValueError, ZeroDivisionError