为什么 print(__name__) 给出 'builtins'? [英] Why does print(__name__) give 'builtins'?
问题描述
我正在使用 pycharm.2017.1.2.我在 py3 环境中安装了 anaconda2.在 Pycharm 中,我使用的是 Python3 解释器,代码很简单:
I'm using pycharm.2017.1.2. I installed anaconda2 with py3 environment. in Pycharm, I'm using Python3 interpreter, and the code is simply:
print(__name__)
在 Pycharm 的 Python 控制台中,它会打印 builtins
.
In Python console in Pycharm, it prints builtins
.
如果我单击运行"按钮,它会按预期打印 main
.
If I click the 'run' button, it prints main
as expected.
为什么 PyCharm Python 控制台打印 builtin
而不是 main
?
Why does the PyCharm Python console print builtin
instead of main
?
推荐答案
PyCharm Python 控制台实际上正在运行一个名为 pydevconsole.py
的模块(应该位于 C:\Program Files\JetBrains\PyCharm2017.1.2\helpers\pydev\pydevconsole.py).此模块创建一个解释器并将其设置为 builtins
模块的属性.
PyCharm Python console is actually running a module called pydevconsole.py
(should be located in C:\Program Files\JetBrains\PyCharm 2017.1.2\helpers\pydev\pydevconsole.py). This module creates an interpreter and sets it as an attribute to the builtins
module.
因此 print(__name__)
在 PyCharm 控制台上将显示 builtins
.
Thus print(__name__)
on PyCharm console will show builtins
.
在执行期间(当您单击运行"时),将调用您指定的 Python 解释器,而不是 pydevconsole
模块.因此 print(__name__)
按预期显示 __main__
.
During execution (when you click 'run'), your specified Python interpreter is called, not the pydevconsole
module. therefore print(__name__)
shows __main__
as expected.
这篇关于为什么 print(__name__) 给出 'builtins'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!