Take the 2-minute tour ×

I have a structure such this works :

import a.b.c

and this also works :

from a.b import c

but this doesn't work :

from a import b.c

nor does :

from a import b

How can I do the import so that b.c.foo() works?

share|improve this question
Is there a particular reason why you need the b.c.foo() syntax instead of c.foo() for your code? –  Amber Aug 4 '09 at 2:37
yes, the prefix is really long with lots of nested modules, but importing 'c' is a namespace collision. –  Paul Tarjan Aug 4 '09 at 2:40
ok, adding to that, assume that it is more semantically correct to write c.foo() so that we can't skirt the problem. –  Paul Tarjan Aug 4 '09 at 3:02
namespace collisions are easier to solve with a from a.b import c as b_c and similar renamings. –  Alex Martelli Aug 4 '09 at 3:02

4 Answers 4

up vote 9 down vote accepted

Just rename it:

from a.b import c as BAR

share|improve this answer
see newest comment. but indeed, your solution works. –  Paul Tarjan Aug 4 '09 at 3:03

In your 'b' package, you need to add 'import c' so that it is always accessible as part of b.

share|improve this answer
Then b has to guess all the submodules that you would ever want to use on it? The code is in c, shouldn't there be a better way? –  Paul Tarjan Aug 4 '09 at 2:35
from a import b
from a.b import c
b.c = c
share|improve this answer
isn't that monkeypatching the code? –  Paul Tarjan Aug 4 '09 at 2:39
Not sure what "monkeypatching the code" means -- it's monkeypatching module (package) b to remedy the fact that it doesn't, apparently, import its own c sub-module. –  Alex Martelli Aug 4 '09 at 3:01
BTW, the intermediate name could also be changed with an as, of course, whether you then monkeypatch it back in as b.c or not. –  Alex Martelli Aug 4 '09 at 3:03
import a.b.c
from a import b

The order of the import statements doesn't matter.

share|improve this answer

Your Answer


