mypac.py:
def myfunc():
return 'this is a function of mypac'
if __name__=='__main__':
print('executing '+__name__)
print(myfunc())
else:
print('executing '+__name__)
main.py:
import mypac
print('--------')
print(mypac.myfunc())
执行mypac.py:
executing __main__
this is a function of mypac
执行main.py:
executing mypac
--------
this is a function of mypac
执行完main.py后生成这个: