3

I'm trying to run a program that imports simplejson. When I run it in Python 2.7 it's ok, but when I run it in Python 3.3 it says:

File "C:\Python33\lib\simplejson__init__.py", line 111, in <module>
from decoder import JSONDecoder, JSONDecodeError
ImportError: No module named 'decoder'

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    How did you install simplejson? The path in your exception looks really wrong... – ThiefMaster Dec 23 '12 at 13:38
  • I downloaded and extracted it. I did the exact same for my python2.7 and it worked. is there anything special I should do? –  Dec 23 '12 at 13:52
  • *Where* did you extract it to? To your python lib folder? That's wrong. – ThiefMaster Dec 23 '12 at 13:55
  • really? i didn't know that, i'm a beginner.. where should i extract it? –  Dec 23 '12 at 13:59
  • Does this answer your question? [What are the differences between json and simplejson Python modules?](https://stackoverflow.com/questions/712791/what-are-the-differences-between-json-and-simplejson-python-modules) – Karl Knechtel Jan 19 '23 at 04:01

2 Answers2

6

There is no need to use the external simplejson library. The json module included in the Python 3 standard library is the exact same module, but maintained as part of the Python distribution. Quoting from the simplejson PyPI page:

simplejson is the externally maintained development version of the json library included with Python 2.6 and Python 3.0, but maintains backwards compatibility with Python 2.5.

Use the following code to switch to simplejson if json isn't present (only for Python 2.5, the library is included in 2.6 and up):

try:
    import json
except ImportError:
    # python 2.5
    import simplejson as json
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Unless he wants some feature that is not in the bundled `json` module ;) – ThiefMaster Dec 23 '12 at 15:04
  • @ThiefMaster: For python 2.7 and 3.3, the Python version is fully up-to-date (revisions newer than the `simplejson` github copy); the `simplejson` library is a *backport* of the python code these days, I believe. – Martijn Pieters Dec 23 '12 at 15:15
  • 2
    simplejson is able to convert Decimal to json with `json.dumps(Decimal(1))`, while the python 3.6 version cannot. Its these infinite small breaking changes that mean we're still looking at Python 2 after 10 years. – Kirk Jun 07 '18 at 17:05
  • @Kirk this is burning me right now with Python 3.8, nearly 3 years later. Infuriating. – John Lyon Mar 07 '21 at 22:41
  • @JohnLyon so add a `default` hook that handles Decimal objects. The problem is that not everyone agrees on how these would be converted to JSON; as string or as float. Or how to *decode* back to Decimal again; when encoded as string, how to distinguish between non-Decimals and Decimal, as float you lost precision. – Martijn Pieters Mar 08 '21 at 00:01
  • @MartijnPieters if I have a decimal value of 3.6, how is it ambiguous as to how that should be serialized into JSON? It's deserializing it that's the issue, and I'm not doing that. `simplejson` v 3.17.2 can handle doing this, but the `json` builtin in python 3.8 cannot. And `requests` still uses the standalone `simplejson` and handles Decimals if it is installed. It's all a bit of a mess IMO. – John Lyon Mar 10 '21 at 00:51
  • @JohnLyon: because serialising it to a string makes assumptions about how you'll decode it again. You can't just dump the value into a string without some kind of agreement that _that specific value is a [real number](https://en.wikipedia.org/wiki/Real_number), and not just a string. Similarly, if you encode to a number representation (which simplejson does, using the `str(Decimal)` output, you need to make sure your _decoder_ doesn't clobber the precision by decoding to a floating point type. – Martijn Pieters Mar 10 '21 at 10:34
  • @JohnLyon: Sure, you as a developer can be happy that there is no exception now, that the 'problem has gone away', but you've just shifted it somewhere else. The Python standard library choose not to force the choice; your application will have to make an explicit decision on how to represent `Decimal` values, and that's not a difficult job to achieve, just define a `default` hook that handles `Decimal`s. – Martijn Pieters Mar 10 '21 at 10:35
1

You need to install it properly. That means using easy_install simplejson or pip install simplejson. Since you are on windows neither of these command-line tools are installed by default.

However, there is also a half-automated way install a package properly: Download and unpack it to some temporary folder and then open a command line window inside the package's folder and execute python setup.py install in there.

Extracting it manually to your Python folder is pretty much always a bad choice that is likely to mess up your python installation (in case any conflicts with existing files occur).

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • thank youuuuu. i installed it finally. but it's installed in my python2.7 directory. is there a way i can change the path to my python3.3 directory? –  Dec 23 '12 at 15:03
  • Yes. Use the `python` binary from Python 3. But consider @MartijnPieters' solution. That is most likely the better choice in your case. – ThiefMaster Dec 23 '12 at 15:04