I have a package structure like:
thePackage/
__init__.py
moduleA.py
moduleB.py
moduleC.py
The __init__.py file contains
from . import moduleA
For simplicity I cd into the directory containing the foler thePackage.
When I try to import thePackage I get the error:
Traceback (most recent call last):
File "<ipython-input-10-5fe9a18b3124>", line 1, in <module>
import thePackage
File "C:\thePackage\__init__.py", line 2, in <module>
from . import moduleA
ImportError: cannot import name 'moduleA'
I've read a few posts like
Importing packages in Python, but still can't figure out my problem. The accepted answer in that post suggests I should be able to import the submodule moduleA by import thePackage.moduleA, but when I try this I get the exact same error.
How can I important the package thePackage?
Also, how can I just import moduleA?
3 Answers 3
from thePackage import moduleA
1 Comment
I encountered the issue while using Spyder and Python3.4.
When I closed and restarted Spyder, I was able to import the package using import thePackage, and I was able to import moduleA using import thePackage.moduleA.
Also, originally when I first tried to import the module using import thePackage, it failed. At this point I might have modified and saved the __init__.py. Then I probably tried to import the module again using import thePackage, but maybe this doesn't always try to import the newly saved versions of thePackage. Thus, I recommend trying
import importlib
importlib.reload(thePackage)
to force the import of the newly saved version of thePackage.
Comments
you could try
sudo apt-get install python-setuptools
found it at https://bitbucket.org/pypa/setuptools/issues/368/module-object-has-no-attribute-packaging, and it helped me when I was having challenges installing pyttsx,