Python: how to import part of a namespace

I have a structure that works:

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

and this also works:

from a.b import c
c.foo()

but this does not work:

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

and does not:

from a import b
b.c.foo()

How can I import to b.c.foo()work?

+3
source share
4 answers

Just rename it:


from a.b import c as BAR

BAR.foo()
+9
source

In your package byou need to add ' import c' so that it is always available as part b.

+2
source
from a import b
from a.b import c
b.c = c
+2
source
import a.b.c
from a import b
b.c.foo()

The order of the import statements does not matter.

0
source

Source: https://habr.com/ru/post/1714274/


All Articles