pythonjsonimportsimplejsonattributeerror

Python JSON module has no attribute 'dumps'


I am running Python 2.7 (x64 Linux) and trying to convert a dict to a JSON object.

>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=0, releaselevel='final', serial=0)

I am trying to use simplejson (falling back to json from the standard library) but I get the following error:

>>> try: import simplejson as json
... except ImportError: import json
...                  
>>> metadata = dict()
>>> metadata['foo'] = 'bar'
>>> print metadata
{'foo': 'bar'}
>>> json.dumps(metadata)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dumps' 

Is there something obvious I am missing about using json or simplejson with Python 2.7?


Solution

  • Turned out I had an old json library loaded from an old Python installation:

    >>> import json                                                                                                                                                                                                                                                                           
    >>> print json.__file__                                                                                                                                                                                                                                                                   
    /home/areynolds/opt/lib/python2.5/site-packages/json.pyc
    

    Removing that old stuff fixed the issue. Thanks!