29.4. __main__ — Top-level script environment - Python 3.5.9 documentation 编辑
'__main__'
is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__'
when read from standard input, a script, or from an interactive prompt.
A module can discover whether or not it is running in the main scope by checking its own __name__
, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m
but not when it is imported:
if __name__ == "__main__": # execute only if run as a script main()
For a package, the same effect can be achieved by including a __main__.py
module, the contents of which will be executed when the module is run with -m
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论