[英]Import function from imported module
I have a module with a long name.我有一个名字很长的模块。 I want to import some functions from it, using
from ... import ...
syntax.我想从中导入一些函数,使用
from ... import ...
语法。 I tried to make these imports shorter, using import ... as ...
trick, but it didn't work.我试图使这些导入更短,使用
import ... as ...
技巧,但它没有用。 Why this example doesn't work?为什么这个例子不起作用?
>>> import long_named_module as spam
>>> from spam import func
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named spam
The best way to do that is:最好的方法是:
import long_named_module as spam
func = spam.func
from long_named_module import func1, func2, func3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.