2

I am having a problem importing from a specific module, but not importing from modules in general. My project structure:

project-folder
  aaa-folder
    __init__.py
    a.py
  bbb-folder
    __init__.py
    b.py
  ccc-folder
    __init__.py
    c.py

All the init.py files are blank. The project-folder is in my sys.path.

module b:

from aaa.a import a_method
from ccc.c import c_method

def b_method(): print 'bye'
print 'hi'

module c:

from aaa.a import a_method
from bbb.b import b_method

def c_method(): print 'bye'
print 'hi'

module a:

from bbb.b import b_method
from ccc.c import c_method

def a_method(): print 'bye'
print 'hi'

I can import every method from the python shell. Module b and c both run from the command line and simply return 'hi'. When I run module a, I get ImportError: No module named b

Where might I find clues to my problem?

Cole
  • 2,489
  • 1
  • 29
  • 48
  • make sure you set your python path to include the root folder. – Joran Beasley Nov 25 '12 at 23:52
  • This is using circular imports – jdi Nov 25 '12 at 23:54
  • 2
    You have circular references (A needs B and C which both need A first). It’s a sign for bad design. – poke Nov 25 '12 at 23:54
  • The circularity as a sign of bad design, I get, and will restructure the lay out of the modules in a more organized manner. None for the module methods have any dependences on other methods in other modules. Even with this independence, would I trigger a circularity error? – Cole Nov 26 '12 at 00:03

1 Answers1

2

As mentioned a few times in the comments, you are designing you project to use circular imports:

Circular imports are fine where both modules use the “import ” form of import. They fail when the 2nd module wants to grab a name out of the first (“from module import name”) and the import is at the top level. That’s because names in the 1st are not yet available, because the first module is busy importing the 2nd.

If you place a print statement at the very top of each of those modules, you will see what is happening...

Example a,b,c: print "inside aaa" # bbb, ccc

When you execute module a, you will get this:

$ python aaa/a.py 
inside aaa
inside bbb
inside aaa
Traceback (most recent call last):
  File "aaa/a.py", line 2, in <module>
    from bbb.b import b_method
  File "/Users/user/Desktop/project/bbb/b.py", line 2, in <module>
    from aaa.a import a_method
  File "/Users/user/Desktop/project/aaa/a.py", line 2, in <module>
    from bbb.b import b_method
ImportError: cannot import name b_method

a.py goes to import b.py, but b needs a, so it goes back to that module, where it sees the rest of the bbb import. Well b was only partial loaded at this point, so b_method is not yet defined. So you get a crash.

What you should do is avoid having your modules import each other in circles. There should be a set of common modules for which others all import the shared functionality. But as stated in the python faq, it can be worked around by only importing the module namespace (import bbb).

jdi
  • 90,542
  • 19
  • 167
  • 203
  • Thanks for the detailed explanation! I pulled the method in question out of the module I wrote it in and put it in a module which contains common methods. I asked my question so I could understand the details of what caused the ImportError and your answer was perfect for that! – Cole Nov 26 '12 at 00:29