Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

`ImportError` from python submodule

I have the following structure for my Python modules:

.
├── a
│  ├── __init__.py
│  ├── aa.py
│  └── b
│  ├── __init__.py
│  └── bb.py
└── root.py

Contents of aa.py

from b.bb import hello_bb
def hello_aa():
 hello_bb()
 print("hello from aa.py")

Contents of bb.py

def hello_bb():
 print("hello from bb.py")

Contents of root.py

from a.aa import hello_aa
hello_aa()

With Python 3.5.1, executing python root.py gives the following error:

Traceback (most recent call last):
 File "root.py", line 1, in <module>
 from a.aa import hello_aa
 File ".../a/aa.py", line 1, in <module>
 from b.bb import hello_bb
ImportError: No module named 'b'

Both the __init__.py are empty.

Anything else I am missing to make this work?

Answer*

Draft saved
Draft discarded
Cancel
2
  • I don't have to use relative import in root.py. Any ideas why it works there? Commented Sep 8, 2017 at 21:16
  • 2
    @armundle, I think it is because there is no __init__.py in the directory of root.py. BTW this directory should not be considered as a package because of this. You have module root.py and package a with submodule b. Commented Sep 8, 2017 at 21:20

lang-py

AltStyle によって変換されたページ (->オリジナル) /