3

I have a structure such this works :

import a.b.c a.b.c.foo() 

and this also works :

from a.b import c c.foo() 

but this doesn't work :

from a import b.c b.c.foo() 

nor does :

from a import b b.c.foo() 

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

4
  • Is there a particular reason why you need the b.c.foo() syntax instead of c.foo() for your code? Commented Aug 4, 2009 at 2:37
  • yes, the prefix is really long with lots of nested modules, but importing 'c' is a namespace collision. Commented Aug 4, 2009 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. Commented Aug 4, 2009 at 3:02
  • 2
    namespace collisions are easier to solve with a from a.b import c as b_c and similar renamings. Commented Aug 4, 2009 at 3:02

4 Answers 4

9

Just rename it:

 from a.b import c as BAR BAR.foo() 
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

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?
2
from a import b from a.b import c b.c = c 

2 Comments

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.
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.
0
import a.b.c from a import b b.c.foo() 

The order of the import statements doesn't matter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.