# What gets printed if foo is the main programbefore importbefore function_abefore function_bbefore __name__ guardFunction AFunction B 10.0after __name__ guard
# What gets printed if foo is imported as a regular modulebefore importbefore function_abefore function_bbefore __name__ guardafter __name__ guard
# file one.pydef func():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")
# file two.pyimport one
print("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")
现在,如果您调用解释器作为
python one.py
输出将是
top-level in one.pyone.py is being run directly
如果你运行two.py:
python two.py
你得到
top-level in one.pyone.py is being imported into another moduletop-level in two.pyfunc() in one.pytwo.py is being run directly
def main():"""business logic for when running this module as the primary one!"""setup()foo = do_important()bar = do_even_more_important(foo)for baz in bar:do_super_important(baz)teardown()
# Here's our payoff idiom!if __name__ == '__main__':main()
if __name__ == '__main__':# Do something appropriate here, like calling a# main() function defined elsewhere in this module.main()else:# Do nothing. This module has been imported by another# module that wants to make use of the functions,# classes and other useful bits it has defined.
import abdef main():print('main function: this is where the action is')def x():print ('peripheral task: might be useful in other projects')x()if __name__ == "__main__":main()
# Other modules can IMPORT this MODULE to use the function fibdef fib(n): # write Fibonacci series up to na, b = 0, 1while b < n:print(b, end=' ')a, b = b, a+bprint()
# This allows the file to be used as a SCRIPTif __name__ == "__main__":import sysfib(int(sys.argv[1]))
apple = 42
def hello_world():print("I am inside hello_world")
if __name__ == "__main__":print("Value of __name__ is: ", __name__)print("Going to call hello_world")hello_world()
我们可以直接执行这个作为
python test.py
产出
Value of __name__ is: __main__Going to call hello_worldI am inside hello_world
现在假设我们从另一个脚本调用上面的脚本:
脚本external_calling.py
import test
print(test.apple)test.hello_world()
print(test.__name__)
if __name__ == "__main__":print("file1 is being run directly")else:print("file1 is being imported")
创建*file2.py
import file1 as f1
print("__name__ from file1: {}".format(f1.__name__))print("__name__ from file2: {}".format(__name__))
if __name__ == "__main__":print("file2 is being run directly")else:print("file2 is being imported")
执行file2.py
产出:
file1 is being imported__name__ from file1: file1__name__ from file2: __main__file2 is being run directly
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':print('This is going to be printed out, only if user invokes the module as a script')
第一种可能:在另一个模块中导入my_test_module.py
# main.py
import my_test_module
if __name__ == '__main__':print('Hello from main.py')
现在如果你调用main.py:
python main.py
>> 'This is going to be printed out, no matter what'>> 'Hello from main.py'
python my_test_module.py
>>> 'This is going to be printed out, no matter what'>>> 'This is going to be printed out, only if user invokes the module as a script'
def func():# Do somethingpass
def func2():# Do somethingpass
print('The program name is set to ', globals()['__name__'])
if __name__=='__main__':# In the current program, __name__ is equal to '__main__'func('https://www.wikipedia.org')func2('https://www.wikipedia.org')# Or do more jobs
import test1print('inside of current program')print('name is current program', __name__)print(globals()['test1'])test1.func('another site')test1.func2('another site')
产出
inside of test 1name of program is set to test1end of moduleinside of current__main__<module 'test1' from 'C:\\users\\ir\\appdata\\local\\programs\\python\\python38\\lib\\test1.py'>
def main():n = int(input('Write a number: '))a, b = 0, 1while b < n:a, b = b, a+bprint('Fibonacci number %i: %i' % (n, b))
if __name__ == "__main__":main()