Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does print(__name__) give 'builtins'?

Tags:

python

pycharm

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__)

In Python console in Pycharm, it prints builtins.

If I click the 'run' button, it prints main as expected.

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

like image 770
user8641707 Avatar asked Feb 28 '18 13:02

user8641707


2 Answers

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.

Thus print(__name__) on PyCharm console will show builtins.

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

like image 98
Joe Avatar answered Nov 15 '22 08:11

Joe


This happens if the run environment is remote, and that IPython isn't installed. It's a PyCharm issue reported here. It happened in 2018.2 release, and a fix is in progress. To solve it just install IPython in the remote environment.

like image 21
CharlesB Avatar answered Nov 15 '22 08:11

CharlesB