Python3:无法从json.decoder导入JSONDecodeError

use*_*115 5 python simplejson python-3.x

我使用python 3.4.3设置virtualenv并尝试从json.decoder导入JSONDecodeError

from json.decoder import JSONDecodeError (I think its valid in python3 ? But why not working for me ?) 
Run Code Online (Sandbox Code Playgroud)

但它没有用.只有下面工作:

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

我怎么做的?

virtualenv venv --no-site-packages -p python3 
pip install ipython
ipython
from json.decoder import JSONDecodeError
ImportError: cannot import name 'JSONDecodeError'
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Łuk*_*ski 16

根据3.4.x docs,ValueError当JSON解码失败时会引发plain .

JSONDecodeErrorclass从3.5.x开始提供.

  • 确实.由于`JSONDecodeError`继承自`ValueError`,如果你关心可移植性,最好抓住后者. (4认同)