简体   繁体   English

如果 __name__ == "__main__":

[英]If __name__ == "__main__":

In .py file I have some functions which are being called in if name == " main ": other than this, chrome driver is also initialized there.在 .py 文件中,我有一些函数在 if name == " main " 中被调用:除此之外,chrome 驱动程序也在那里初始化。 def func1: def func2:定义函数1:定义函数2:

if __name__=="__main__":

    chromedriver

    value = func1()
    func2()

Now, I want to call this whole file/module in another file.现在,我想在另一个文件中调用整个文件/模块。 what should I have to do?我该怎么办? What is the easiest way for this?什么是最简单的方法?

Move everything underneath the if __name__=="__main__": into a main() method and call that from if __name__=="__main__":if __name__=="__main__":下的所有内容移动到main()方法中,并从if __name__=="__main__":调用它if __name__=="__main__":

def main():
    driver = create_chrome_driver()
    value = func1()
    func2()


if __name__=="__main__":
    main()

And call it as well as from your other file:并从您的其他文件中调用它:

import mymodule
mymodule.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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