问题
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?
回答1:
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.
回答2:
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!
回答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.
回答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'>
回答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
回答6:
I create a file named json.py. When I run this I got the error, so I rename it and it words for me.
回答7:
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.
回答8:
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'.
回答9:
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.
回答10:
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
来源:https://stackoverflow.com/questions/11369734/python-json-module-has-no-attribute-dumps