简体   繁体   中英

ImportError: cannot import module - Only one module can not be imported from package

I created a package, for the ease of use I call it packageA.

In this package I have 4 submodules in (separate files) and an init file, so the package contains the following files:

  • __init__.py
  • moduleA.py
  • moduleB.py
  • moduleC.py
  • moduleD.py

Module BD stand alone, they don't import any other local module. Module A imports module BD.

Now I have a script that wants to import packageA , the init.py is empty so far.

import packageA

works without problems. But moduleA is not available from this import. If I want to use it via packageA.moduleA it raises this error:

AttributeError: 'module' object has no attribute

The following also works without problems:

from packageA import moduleB
from packageA import moduleC
from packageA import moduleD

And the next import causes the ImportError:

from packageA import moduleA

raises this Error:

Traceback (most recent call last):
File "run.py", line 19, in <module>
from packageA import moduleA
ImportError: cannot import name moduleA

I thought that maybe I'm doing sth wrong in moduleA, but even if moduleA is empty the import raises the same error. It seems like that this module can't be imported somehow. How is this possible?

I have separete unit test files for all python modules, and yes, moduleA is working correctly without any errors.

Perhaps you are importing a different version of the package from a different location. Try this in your script to make sure it is the right one -- it should print the full path of the __init__.py file.

import packageA
print packageA.__file__

Also, check if the package's module search path has been altered. Normally this is a list that contains one element, the directory path of the package:

print packageA.__path__

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM