模块的导入与执行

来源:互联网 发布:淘宝权为什么被禁赛 编辑:程序博客网 时间:2024/06/03 12:42
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 testtest.say_morning()# -> Good morning test

当执行的时候,执行了test.py中的if语句,当在Python中导入时,__name__自动复制为模块名,即test。