尝试使用pprint时出现“TypeError:'module' object is not callable”错误

37

我已经尝试了这段代码以使一个dict更易读:

import pprint

pprint({})

这将抛出以下错误:

Traceback (most recent call last):
  File "temp.py", line 3, in <module>
    pprint({})
TypeError: 'module' object is not callable

为什么它不能被调用?


1
错误信息已经说明了:因为pprint是一个模块(而不是函数)。 - Chris Martin
1个回答

60

尝试使用以下方法导入:

from pprint import pprint

pprint() 函数在 pprint 模块中。


感谢@JamieBull的回答!对于我来说,在Jupyter Lab中,我需要编写pprint.pprint()使其可调用:with open("somefile.json", "r") as src: f = json.load(src) pprint.pprint(f)然而,在Visual Studio Code中,我只需要编写pprint(f)即可。我不完全明白为什么。 - TimB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接