Here's a mysterious python problem:
I'm developing a python package that occasionally reports import errors looking like ImportError: cannot import name …
. The modules it cannot import generally
I have been able to reproduce a similar effect with this simple example:
mypkg/__init__.py
:
from . import module_a
yarg ## cause import error
mypkg/module_a.py
:
print "imported module_a"
Now I will attempt to import the package twice. Notice that the error changes on the second import:
>>> import mypkg
Module A imported
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg/__init__.py", line 2, in <module>
yarg
NameError: name 'yarg' is not defined
>>> import mypkg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg/__init__.py", line 1, in <module>
from . import module_a
ImportError: cannot import name module_a
What gives?
Note:
sys.modules['mypkg.module_a']
after the first import, then the second import gives me back the original error messageIf the error occurs due to a circular dependency, it can be resolved by moving the imported classes to a third file and importing them from this file. If the error occurs due to a misspelled name, the name of the class in the Python file should be verified and corrected.
The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.
That can be caused by too many fields or records in the file, too many columns, or too many rows. The import error can be caused by limits set by the program using the file or the amount of available memory on the system.
I can illustrate what is causing the difference between each import
, but I'm not expert enough on Python's import process to be able to explain the why very well.
>>> import sys
>>> before_import = set(sys.modules.keys())
>>> import mypkg
imported module_a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg\__init__.py", line 2, in <module>
yarg ## cause import error
NameError: name 'yarg' is not defined
>>> after_import = set(sys.modules.keys())
>>> after_import.difference(before_import)
set(['mypkg.module_a'])
When you import mypkg
, it successfully imports module_a
and adds it to sys.modules
. Then mypkg
errors and doesn't get added itself to the sys.modules
dictionary. Deleting the entry allows you to reimport with the same error:
>>> import sys
>>> del sys.modules['mypkg.module_a']
>>> import mypkg
imported module_a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg\__init__.py", line 2, in <module>
yarg ## cause import error
NameError: name 'yarg' is not defined
Now, what I think is happening is:
import mypkg
starts the import process for mypkg
As it's processing mypkg
, it successfully imports module_a
as
a subpackage of itself and adds it to sys.modules
When it hits the error, the import process for mypkg
fails and no
entry for mypkg
is left in sys.modules
The conjunction of the package failing but the subpackage succeeding conflicts with subsequent imports
That's about the best I can fathom, sorry. Python's import process is something of a black art.
I'm pretty sure the problem is that your package is failing to load. You've put some nonsense (yarg
by itself) in the __init__.py
file. This means that mypkg
can't be imported. Because of this, mypkg.module_a
can't be imported either.
I suspect you get different errors because Python is doing some caching of the module state. The first time you try importing mypkg
the import of its submodule module_a
is allowed even though mypkg
is in the process of being loaded. The second time, the fact that mypkg
doesn't work right is cached, so mypkg.module_a
fails to load since its parent package is broken.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With