vim test.py # 建立test.py
# 在vim剪辑器中编辑以下四行内容
def say_morning():
print('Good morning %s' % __name__)
if __name__ == '__main__':
say_morning()
chmod u+x test.py # 修改test.py为可执行文件
当使用python test.py
执行的时候输出Good morning __main__
,当在Python中导入时,如下
import test
test.say_morning()
# -> Good morning test
当执行的时候,执行了test.py中的if语句,当在Python中导入时,__name__自动复制为模块名,即test。