What this error means
You tried to import a specific name (function, class, or variable) from a module, but that name doesn't exist in the module. The module might exist, but it doesn't have what you're looking for.
Common causes
- You misspelled the name you're trying to import.
- The name doesn't exist in that module (maybe it's in a different module).
- The module exists but doesn't export that name (it's private or not defined).
- Circular import issue (module A imports from B, B imports from A).
Example that causes the error
from math import sqr # ImportError: should be sqrt, not sqr
from datetime import current_time # ImportError: doesn't exist
How to fix it
Check the correct name and module. Use dir() to see what's available:
import math
print(dir(math)) # Shows all available names in math module
from math import sqrt # OK: sqrt exists
from datetime import datetime # OK: datetime exists
Import the whole module first
import math
result = math.sqrt(16) # OK: access via module name
# Or check documentation for correct names
Handle import errors gracefully
try:
from mymodule import myfunction
except ImportError:
print("Could not import myfunction")
myfunction = None # Set a default