27

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
frazman
  • 32,081
  • 75
  • 184
  • 269

4 Answers4

32

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!

Mark
  • 18,730
  • 7
  • 107
  • 130
27

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

from json import JSONDecodeError

No need for simplejson package

George Bikas
  • 1,312
  • 12
  • 18
3

Upgrade your installation:

$ pip install -U simplejson
$ python
>>> from simplejson import JSONDecodeError
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

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.

dsh
  • 12,037
  • 3
  • 33
  • 51