Reputation: 377
I have a simple Python import question. I have a module (let's call it A) that is importing a module B. Module B imports a lot of other modules C, D, E, F, etc. I want module A to be able to use the modules C, D, E, F, etc. Is there an easy way to do this? I don't want to directly import C, D, E, F, etc from A.
What I'm trying to accomplish is to provide an API to script developers, who write module A. So, I wanted module A only to have to include module B which is the main entry point to the API and imports all the modules in the API.
Upvotes: 1
Views: 310
Reputation: 1
You can use from ... import *
in both A and B files.
After this all definition either in B or C or D can be used without confusion.
A.py :
from B import *
fun_c()
B.py :
from C import *
C.py:
def fun_c():
pass
Upvotes: 0
Reputation: 636
You can use from ... import *
in B. After this, all definitions from C, D will be available in B namespace. But this solution is not always convenient for a large amount of code, it leads to confusion.
a.py:
import b
b.foo()
b.py:
from c import *
from d import *
c.py:
def foo():
pass
Upvotes: 0
Reputation: 64008
You could do...
from B import *
...inside module A (although you need to make sure that there's nothing else inside module B that has the same name with anything inside module A, otherwise you'll have a namespace collision).
Once you do that, you could do...
C.blah()
D.testing.meh()
...inside module A, as usual.
Alternatively, a better solution would be to do something like the below in module A:
import B
B.C.blah()
B.D.testing.meh()
Upvotes: 2