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 B-D stand alone, they don’t import any other local module. Module A imports module B-D.
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__.pyfile.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: