[Language Infrastructure] I just want to import a module target package, did not expect the other non-target package module code has been executed. .

Problem Description:

Directory Structure

top
main.py
└ target.py

Code

#----main.py
import target
target.fun()
'''
输出结果
C:\Users\Administrator\Desktop\top>python main.py
target-other
target-function
'''

#----target.py
def fun():
    print('target-function')
print('target-other')

Introducing visible target module when the module execution code will pass the non-

 

solution

if __name__=='__main__':

  pass

Guess you like

Origin www.cnblogs.com/remly/p/11546201.html