Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 844
Alex Reynolds Avatar asked Jul 06 '12 21:07

Alex Reynolds


1 Answers

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.

like image 106
Paulo SantAnna Avatar answered Sep 24 '22 17:09

Paulo SantAnna