Python:无法导入JSONDecodeError

Moh*_*hit 9 python macos python-import

我正在尝试跟进:

from simplejson import JSONDecodeError
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

from simplejson import JSONDecodeError
ImportError: cannot import name JSONDecodeError
Run Code Online (Sandbox Code Playgroud)

以下信息可能有所帮助:

  • 这段代码在ubuntu中运行正常,但我在mac中遇到此错误.

  • 我有多个版本的python,我刚刚删除了python 2.6(因为我使用的是python 2.7)

  • 并用于easy_install_27安装此特定库.

Mar*_*ark 17

您已经有了如何获取JSONDecodeError的答案,但我觉得正确的建议应该是您不应该尝试导入它.

原因是JSONDecodeError只出现在simplejson,并且除非你的Python版本严重过时,否则没有理由使用它.内置json版本在最近的版本中同样快,并且没有unicode错误.信息:https://stackoverflow.com/a/16131316/723090

解决方案:json引发ValueError而不是JSONDecodeError,但JSONDecodeError(引发simplejson)是ValueError的子类.所以你可以简单地除了一个ValueError,它将适用于jsonsimplejson!


Geo*_*kas 9

只是为了更清楚@tim的评论,在python3中你可以写

from json import JSONDecodeError
Run Code Online (Sandbox Code Playgroud)

不需要 simplejson 包


dsh*_*dsh 1

它在我的电脑上运行:

$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from simplejson import JSONDecodeError
>>> 
Run Code Online (Sandbox Code Playgroud)

您是否确认正在运行安装了 simplejson 库的 python 安装?检查sys.path并验证所有预期位置是否都在搜索路径中。有效吗import simplejson?如果是这样,请验证模块是从哪个文件加载的 ( import simplejson; print simplejson.__file__)。如果符合预期,则验证模块的内容并查看其中是否存在 JSONDecodeError 类。