1

I'm using Python 3.5.2, PyCharm Community Edition 2017.1.1 in Ubuntu 16.04.2 LTS

I'm a beginner in Python and I'm trying to get this code to work.

a = getattr(__builtins__, 'print')

Actually it works fine in IDLE, but Pycharm is not recognising the builtins. Other common builtins functions like str(), int()... are woking fine.

I searched in Pycharm support for solutions.

The "Reload" button in Settings | Python Interpreters regenerates the skeletons for binary modules, including builtins. Please try pressing it.

But its not working too. I get this in Pycharm...

AttributeError: 'dict' object has no attribute 'print'

Thank you in advance.

Goutham Ganesan
  • 907
  • 2
  • 8
  • 18

1 Answers1

2

You should not directly use the __builtins__ and instead use the builtins module.

In the pycharm python console, the __builtins__ acts as a dict, which in that case, you will need to retrieve print by a = __builtins__['print']. In contrast, if you run it as a script, the __builtins__ act as the module representation the builtinsΓΈ module. In that case, you will need to do it your way using either a = getattr(__builtins__, 'print') or a = __builtins__.print.

But as I stated before, you should not directly use the __builtins__ keyword. You should do this instead:

import builtins
a = builtins.print # or getatrr(builtins, 'print') which either way you prefer

Perhaps also check this answer.

Community
  • 1
  • 1
Taku
  • 31,927
  • 11
  • 74
  • 85