Skip to content

ImportError: cannot import name 'X' from 'Y'

Error message

                cannot import name 'sleep' from 'time'
              

Quick summary

You tried to import a name from a module, but the name doesn't exist in that module or there's a circular import.

Why this happens

  • The name doesn't exist in the module (typo or wrong module)

  • Circular imports — two modules import each other

  • The module exists but the specific name was removed or renamed

  • Relative import issues in packages

Minimal example

✗ Broken code
from time import sleepp  # ImportError — typo in 'sleep'
✓ Fixed code
from time import sleep  # Correct name
# or
import time
time.sleep(1)

The name 'sleepp' doesn't exist in the time module. Fix the typo to 'sleep'.

How to diagnose

  • Check the module's contents with dir(module) or the documentation

  • Verify the name spelling

  • Look for circular import patterns

How to fix

  • Fix the import to use the correct name

  • Restructure code to avoid circular imports

  • Use the full module path instead of from...import

  • Import inside a function to break circular dependencies

How to prevent

  • Use IDE autocomplete to verify import names

  • Structure modules to avoid circular dependencies

  • Keep imports at the top of the file

Related Resources

Related Glossary

Related Lessons

← Back to language