I am trying to make a following call:
from simplejson import JSONDecodeError
But I am getting this error:
from simplejson import JSONDecodeError
ImportError: cannot import name JSONDecodeError
The following information may help:
This code runs fine in ubuntu but i get this error in mac.
I had multiple versions of python and I just erased python 2.6 (as i am using python 2.7)
and used easy_install_27
to install this particular library.
You already have the answer on how to get JSONDecodeError, but I feel that the correct advice should be that you shouldn't try to import it.
The reason is that JSONDecodeError appears only in simplejson
, and there's not really a reason to use that unless your Python version is severely outdated. The built-in json
is just as fast in recent versions, and has no unicode bug. Info: https://stackoverflow.com/a/16131316/723090
The solution: json
raises a ValueError instead of JSONDecodeError, but JSONDecodeError (raised by simplejson
) is a subclass of ValueError. So you could simply except a ValueError and it'll work for json
and simplejson
!
Just to make more clear the comment of @tim, in python3 you can just write
from json import JSONDecodeError
No need for simplejson package
Upgrade your installation:
$ pip install -U simplejson
$ python
>>> from simplejson import JSONDecodeError
It works on my computer:
$ 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
>>>
Have you confirmed that you are running the installation of python in which the simplejson library is installed? Check sys.path
and verify that all of the expected locations are in the search path. Does import simplejson
work? If so, verify from what file the module was loaded (import simplejson; print simplejson.__file__
). If that is as expected, then verify the contents of the module and see if the class JSONDecodeError exists in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With