Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import JSONDecodeError

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.

like image 884
frazman Avatar asked Mar 02 '13 04:03

frazman


4 Answers

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!

like image 117
Mark Avatar answered Nov 05 '22 11:11

Mark


Just to make more clear the comment of @tim, in python3 you can just write

from json import JSONDecodeError

No need for simplejson package

like image 45
George Bikas Avatar answered Nov 05 '22 11:11

George Bikas


Upgrade your installation:

$ pip install -U simplejson
$ python
>>> from simplejson import JSONDecodeError
like image 3
Burhan Khalid Avatar answered Nov 05 '22 13:11

Burhan Khalid


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.

like image 1
dsh Avatar answered Nov 05 '22 13:11

dsh