[英]Error when I try to import a module
This question may be trivial but I don't really get it. 这个问题可能很琐碎,但我真的不明白。 I have two python modules.
我有两个python模块。
This is module1
: 这是
module1
:
import module2
def main():
print funcion2(2,3)
if __name__ == '__main__':
main()
This is module2
: 这是
module2
:
def funcion2(a, b):
return a + b
I get an error (function2 wasn't found). 我收到一个错误(找不到function2)。 If write " from module2 import * " it works fine.
如果将“ from module2 import *”写入,则可以正常工作。 Why?
为什么?
If you want to import module2
you will need to call function2
this way: module2.funcion2(2,3)
. 如果要
import module2
,则需要以这种方式调用function2
: module2.funcion2(2,3)
。
You usually want to avoid from <module> import *
so either do as above or from module2 import function2
and then you can simply call function2(2, 3)
. 您通常想避免
from <module> import *
所以可以像上面那样或from module2 import function2
避免,然后可以简单地调用function2(2, 3)
。
Use: 采用:
import module2
module2.funcion2(2, 3)
You import a module and should explicitly specify it while calling a method. 您导入模块,并在调用方法时应明确指定它。
You can also import only this function: 您还可以仅导入以下功能:
from module2 import funcion2
funcion2(2, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.