企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](warnings.xhtml "warnings --- Warning control") | - [上一页](builtins.xhtml "builtins --- 内建对象") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [Python运行时服务](python.xhtml) » - $('.inline-search').show(0); | # [`__main__`](#module-__main__ "__main__: The environment where the top-level script is run.") --- 顶层脚本环境 - - - - - - `'__main__'` 是顶层代码执行的作用域的名称。模块的 \_\_name\_\_ 在通过标准输入、脚本文件或是交互式命令读入的时候会等于 `'__main__'`。 模块可以通过检查自己的 `__name__` 来得知是否运行在 main 作用域中,这使得模块可以在作为脚本或是通过 `python -m` 运行时条件性地执行一些代码,而在被 import 时不会执行。 ``` if __name__ == "__main__": # execute only if run as a script main() ``` 对软件包来说,通过加入 `__main__.py` 模块可以达到同样的效果,当使用 `-m` 运行模块时,其中的代码会被执行。 ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](warnings.xhtml "warnings --- Warning control") | - [上一页](builtins.xhtml "builtins --- 内建对象") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [Python运行时服务](python.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。