Python JSON module has no attribute 'dumps'
Asked Answered
J

10

42

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?

Jacal answered 6/7, 2012 at 21:15 Comment(2)
This worked fine for me on Linux 64bit, but I don't have simplejson. How about print json.__file__?Hildahildagard
try print dir(json) return what?Seanseana
J
21

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!

Jacal answered 6/7, 2012 at 21:24 Comment(0)
A
88

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.

Anzac answered 28/11, 2012 at 18:55 Comment(3)
that's help me to figure out the similar reasonEvanston
Make sure to delete the lingering json.pyc as well!Suzannsuzanna
Take care to also remove the json.pyc (that's PYC) when renamingTheatheaceous
J
21

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!

Jacal answered 6/7, 2012 at 21:24 Comment(0)
U
16

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.

Upholster answered 6/7, 2012 at 21:19 Comment(0)
A
8

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'>
Appoggiatura answered 6/12, 2014 at 0:45 Comment(0)
K
6

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

Kalindi answered 7/4, 2020 at 17:32 Comment(0)
P
5

You may have another script in your Python path called "json", which you are importing by accident. You could resolve this either by renaming the one under your control or using

from __future__ import absolute_import
Platinic answered 27/2, 2013 at 17:30 Comment(0)
D
5

I have fixed the same issue by uninstalling the json_extension library that I added to the project before.

pip uninstall json_extensions
Diatropism answered 10/3, 2021 at 16:13 Comment(1)
Thanks! For me it was: json-extensions and simplejsonPolypoid
C
2

Even I was facing similar error while running json.dump(). In my case I was getting an error string :

AttributeError: 'file' object has no attribute 'dump'

How I fixed it -

I was using variable name as "json" for File descriptor in the same script, which is why I was getting this error. So I simply renamed that variable name and issue resolved.

Cyanamide answered 30/8, 2018 at 9:17 Comment(0)
Y
2

I have discussed the problem and solution below

Problem: when you call the import function on python, python try to find the module inside your working directory, if it does not find inside your local directory, that's when it will look for standard libraries and other global repos. Hence, if you try to import json module, then it'll try to find it inside your directory and If you have a file named 'json.py', python will load this module and tries to find 'dump' function inside your json.py. if it does not find 'dump' function inside your file, it will throw an error 'module' object has no attribute 'dumps'.

Resolution: rename your 'json.py' file into different name: like json_file.py or anything but 'json.py'.

Yl answered 12/8, 2019 at 12:47 Comment(0)
P
0

This error just occurred for me in a different context, but still one of two things named json. I had named a "view" in Django (a Python function that prepares a response to an HTTP request), in this case a view to handle request for data in json format.

But I had named the view "json". Bad move. I was mystified when print dir(json) returned a dumps-free response in my view "json" whereas it showed "dumps" as an attribute in a similar view that worked.

This discussion fixed the problem for me.

Prairial answered 20/11, 2013 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.