0

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?

asked May 28, 2015 at 20:52

3 Answers 3

1
from thePackage import moduleA
answered May 28, 2015 at 21:01
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is correct for how to import the module of a package. However, I had encountered a situation in which this method was failing.
0

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.

answered May 28, 2015 at 21:54

Comments

0

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,

answered Nov 20, 2015 at 11:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.