从不同模块导入函数

2024-04-26 10:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从module-mod导入一个function-func,使用

from mod import func

把它当作

X=func(x,y)

但是在执行函数之前,我的程序正在执行整个模块。 如何使其仅执行函数?你知道吗


Tags: 模块函数fromimport程序modfunctionmodule
1条回答
网友
1楼 · 发布于 2024-04-26 10:27:21

如果要避免模块执行,请将模块中的代码放在main

# stuff to run always here such as class/def
def main():
    pass

if __name__ == "__main__":
   # stuff only to run when not called via 'import' here
   main()

更完整的答案: Why is Python running my module when I import it, and how do I stop it?

相关问题 更多 >