Reputation: 75
I'm trying to import two "sub-modules" that are closely related, and I'm not sure how to import them in a way that shows that they are related. My file structure is as follows:
main.py
module/
child_one/
one.py
child_two
two.py
Is there a way to import one.py
and two.py
into main.py
in a way that has them both as "submodules" of a parent, while shortening the name of the parent? For example something like this (which doesn't work, I think having .
in the name is bad...):
import module.child_one as m.one
import module.child_two as m.two
I think if I didn't want to shorten the name of the parent, I could just import them as module.child_one
and module.child_two
without messing with their names.
Thanks!
Upvotes: 0
Views: 48
Reputation: 101
from your module add __init__.py
file
from that file import the two submodules
__init__.py
from child_one import one
from child_two import two
# or
from child_one.one import func_one
from child_two.two import func_two
you can use them by doing
import module
module.one.func_one()
module.two.func_two()
# or
module.func_one()
module.func_two()
Upvotes: 1
Reputation: 656
class m:
import module.child_one.one as one
import module.child_two.two as two
t = m.one.func_one()
tt = m.two.func_two()
It works, but... unusial
Upvotes: 2
Reputation: 403
You could import the whole module, give it an alias and then call each child.
import module as m
m.child1.foo()
m.child2.foo()
According to the accepted answer here whether you import the whole module or a single function does not make a difference unless you use the child modules in a loop.
Upvotes: 0