首先新建个文件:a.py
def func():
print("func() in a.py")
print("top-level in a.py")
if __name__ == "__main__":
print("a.py is being run directly")
else:
print("a.py is being imported into another module")
直接运行a.py
再新建个文件:b.py
import a as abc
abc.func()
if __name__ == "__main__":
print("b.py is being run directly")
else:
print("b.py is being imported into another module")
运行b.py
理解:__name__ == "__main__" 类似判断该类是不是运行的主文件入口。