deffunc():print("func() in one.py")print("top-level in one.py")if__name__=="__main__":print("one.py is being run directly")else:print("one.py is being imported into another module")
two.py
123456789
importoneprint("top-level in two.py")one.func()if__name__=="__main__":print("two.py is being run directly")else:print("two.py is being imported into another module")
Running python one.py returns:
top-level in one.py
one.py is being run directly
While running python two.py returns:
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly