Python JSON module has no attribute 'dumps'
Solution 1
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!
Solution 2
Had a similar issues, it was caused by another custom module. I named another script
json.py
and it turns out it tried to load the custom json.py file as a module. dumps method is obviously not available there.
Renaming the json.py script to something else (json2.py) got rid of the issue.
Solution 3
Do you have a file named json
or simplejson
in your path that isn't one of those two libraries? If you do, then python will load that file instead of the real library.
Solution 4
How to reproduce this python error:
AttributeError: 'module' object has no attribute 'dumps'
You probably created a file called json.py that was reachable from python's sys.path
. Or you added a directory to your python's sys.path that included a file called json.py.
Option 1: Poison the well by importing json, then importing another module with the same alias:
eric@dev /var/www/sandbox/eric $ python
>>> import json
>>> json.dumps([])
'[]'
>>> import sys as json
>>> json.dumps([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dumps'
Option 2: Poison the well by creating a file called json.py in the python path:
Create a new file json.py, save it. Put this code in there:
def foo():
print "bar"
Open the python terminal and import json:
eric@dev /var/www/sandbox/eric/wsgi $ python
>>> import json
>>> type(json)
<type 'module'>
>>> json.dumps([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dumps'
>>> json.foo()
bar
It's telling you your method isn't there. So ask python to tell you more about the nature of this module and you'll find clues as to who poisoned it.
>>> print json
<module 'json' from 'json.py'>
>>> dir(json)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'foo']
>>> type(json)
<type 'module'>
Solution 5
The mistake i did I name the file name as json.py.
I got the error:
AttributeError: partially initialized module 'json' has no attribute 'dumps' (most likely due to a circular import).
I renamed the file name to json1.py
instead of creating a new file.
Hope it helps
Alex Reynolds
Bioinformaticist, hobbyist iPhone developer, pug caregiver
Updated on July 09, 2022Comments
-
Alex Reynolds almost 2 years
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 tojson
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
orsimplejson
with Python 2.7? -
temple about 10 yearsthat's help me to figure out the similar reason
-
Chris over 7 yearsMake sure to delete the lingering json.pyc as well!
-
bsb over 6 yearsTake care to also remove the
json.pyc
(that's PYC) when renaming -
Andreas over 2 yearsThanks! For me it was: json-extensions and simplejson