Why does printing (__ name__) give "built-in functions"?

I am using pycharm.2017.1.2. I installed anaconda2 with py3 environment. in Pycharm, I use the Python3 interpreter, and the code is simple:

print(__name__) 

In the Python console in Pycharm, it prints builtins .

If I click on the run button, it will print main as expected.

Why does the PyCharm Python console print builtin instead of main ?

+5
source share
1 answer

The PyCharm console Python actually launches a module called pydevconsole.py (should be located in the C: \ Program Files \ JetBrains \ PyCharm 2017.1.2 \ helpers \ pydev \ pydevconsole.py folder). This module creates an interpreter and sets it as an attribute of the builtins module.

Thus, print(__name__) in the PyCharm console will display builtins .

At run time (when you click run), your Python interpreter is called, not the pydevconsole module. therefore print(__name__) shows __main__ as expected.

+3
source

Source: https://habr.com/ru/post/1275623/


All Articles