python - Pycharm 无法识别内置函数

标签 python python-3.x intellij-idea pycharm

我在 Ubuntu 16.04.2 LTS 中使用 Python 3.5.2PyCharm Community Edition 2017.1.1

我是 Python 初学者,我正在尝试让这段代码正常工作。

a = getattr(__builtins__, 'print')

实际上它在 IDLE 中工作正常,但 Pycharm 无法识别内置函数。其他常见的内置函数,如 str()、int()...都运行良好。

我在 Pycharm support 中搜索寻求解决方案。

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

但它也不起作用。我在 Pycharm 中得到了这个...

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

提前谢谢您。

最佳答案

您不应直接使用__builtins__,而应使用builtins 模块。

在 pycharm python 控制台中,__builtins__ 充当字典,在这种情况下,您需要通过 a = __builtins__[' 检索 print打印']。相反,如果您将其作为脚本运行,则 __builtins__ 将充当 builtinsø 的模块表示形式。模块。在这种情况下,您需要使用 a = getattr(__builtins__, 'print')a = __builtins__.print 按照自己的方式进行操作。

但正如我之前所说,您不应该直接使用 __builtins__ 关键字。你应该这样做:

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

也许还可以检查this answer .

关于python - Pycharm 无法识别内置函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405057/

相关文章:

python-3.x - 如何使 tsfresh.extract_features 工作?

ubuntu - 以用户身份运行 Intellij

Android - IntelliJ 使用什么 debug.keystore 文件签署我的应用程序?

python - 如何在 Linux 中分析 PyCuda 代码?

python - 问题是我不能连续播放 2 个声音(即使使用 music.queue)它只播放 1 个音乐并且程序停止,知道吗?

python - Google App Engine 中的 Jinja2

python - TensorFlow 未编译为使用 SSE(等)指令,但这些指令可用

python - 网格搜索的所有参数的组合

javascript - intellij 函数参数有下划线

Python argparse 选项串联