Python3:无法从json.decod导入JSONDecodeError

2024-06-11 07:34:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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 ?) 

但它不起作用。只有下面才有效:

from simplejson import JSONDecodeError

我是怎么做到的?

virtualenv venv --no-site-packages -p python3 
pip install ipython
ipython
from json.decoder import JSONDecodeError
ImportError: cannot import name 'JSONDecodeError'

enter image description here


Tags: infromimportjsonvirtualenvipythonnotpython3
3条回答

根据来自module json (Python version >= 3.5.0)的文档,Python哪个版本的<;3.5.0不像您刚才那样支持import语句,但是如果您使用Python(version>;=3.5.0),那么您的import语句肯定是正确的。

根据3.4.x docs,当JSON解码失败时,将引发普通的ValueError

JSONDecodeError类从3.5.x开始可用。

json是集成到Python中的simplejson版本。它们是分开开发的,已经不一样了。因此,它们不一定可以互换使用。

有关差异的详细信息,请参见this answer

相关问题 更多 >