以下程序为验证代码
#pytorch_test.py
print("I'm the first.")
print(__name__)
if __name__ =='__main__':
print("I'm the second.")
pytorch_test.py的运行结果为:
#test.py
import pytorch_test
print(__name__+'123')
test.py的运行结果
分析可得:作为脚本直接运行时__name__等于__main__;作为模块被调用时,name__为模块名,运行脚本的__name__为__main。
其中,程序中+'123’是为了加以区分。