if __name__ == “__main__“ 讲解
Python 脚本有两种运行方式直接运行本文件python test.py内置变量__name__会被自动赋值为字符串__main__2 被其他文件导入import test__name__等于当前文件名模块名不会等于__main__。这样程序就不会一倒入就直接运行可以用来做测试代码。如下例子print(f当前 __name__ {__name__}) def hello(): print(hello) if __name__ __main__: hello()