我有两个文件:
test2.py
:
def fun():
print "from fun"
print "from test2"
test.py
:
from test2 import fun
print "in text"
fun()
我只想执行 test2.py
中的 fun
函数,但我也从 test2 中获取了 fun
函数.
如何才能只使用我想要的功能?
最佳答案
在 test2.py
中,您需要确保仅在程序自行运行时打印“from test2”。
你可以这样做:
test2.py
def fun():
print "from fun"
if __name__ == "__main__":
print "from test2"
关于python - 如何仅执行另一个文件中的特定函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40449016/