简体   繁体   中英

Python: How to import part of a namespace

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 bcfoo() works?

Just rename it:


from a.b import c as BAR

BAR.foo()

在' b '包中,您需要添加' import c ',以便始终可以将其作为b一部分进行访问。

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

The order of the import statements doesn't matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM