---------------------------------module1.py文件内容---------------------------------
print("I'm from module1.py")
s = "I'm str s and from module1.py"
def func1():
print("I'm func1 and from module1.py")
def func2():
print("I'm func2 and from module1.py")
func1()
def change_s():
global s
s = "I'm str new_s and from module1.py"
print("I'm __name__ and from module1.py: %s" % __name__)
print("I'm __file__ and from module1.py: %s" % __file__)
---------------------------------file_name.py文件内容---------------------------------
import module1
'''
当做脚本执行 __name__ == "__main__"
当做模块导入 __name__ == 模块名
__file__ 当前文件所在路径
'''
print("I'm __name__ and from name1.py: %s" % __name__)
print("I'm __file__ and from name1.py: %s" % __file__)
执行结果:
I'm from module1.py
I'm __name__ and from module1.py: module1
I'm __file__ and from module1.py: F:\2019老男孩周末26期\day05\课下练习\module1.py
I'm __name__ and from name1.py: __main__
I'm __file__ and from name1.py: F:\2019老男孩周末26期\day05\课下练习\file_name.py