Luke
Luke

Reputation: 11644

How to diagnose "ImportError: cannot import name"

I am looking for a systematic approach to dealing with "cannot import name" errors in python. I would like to know

  1. what are all of the situations that generate this message
  2. what can be done to determine the cause of the error.

I know cyclic imports are a common cause, but there seem to be others as well.

Upvotes: 1

Views: 1913

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121864

The traceback is the one way to determine if an import is cyclical.

The other possibility is that the name doesn't exist in the imported module. You either misspelled the name, or you imported a different module from what you expected. Diagnose by running:

import module
print module.__file__

to check if you got the right module. Use dir(module) to check what names it defines.

Upvotes: 2

Related Questions