1

I am getting this error:

Traceback (most recent call last):
File "C:/Users/Shivam/Desktop/jsparse.py", line 13, in <module>
info = json.loads(str(data))
AttributeError: 'module' object has no attribute 'loads'

Any thoughts what wrong I am doing here?

This is my code:

import json
import urllib
url = ''
uh = urllib.urlopen(url)
data = uh.read()
info = json.loads(str(data))
6
  • 2
    Works fine here. Are you sure you don't have another module named json.py? Commented Jul 9, 2016 at 8:03
  • Actually there is no json.py.There is one json.pyc Commented Jul 9, 2016 at 10:22
  • Ah, yeah, remove that. Don't name modules after Python builtin modules :) Commented Jul 9, 2016 at 10:24
  • That was inbuilt.I am using python 2.5.3 and json.pyc was already present in the 'LIB' folder where python is installed.Should I delete that and then run the code?There is no json.py file in 'LIB' Commented Jul 9, 2016 at 10:31
  • Oh, no, don't mess with libraries included with the Python distribution. I meant, was there a module named json in your code. But now I've found your problem and will answer shortly. Commented Jul 9, 2016 at 10:33

1 Answer 1

3

The problem is that you're using Python 2.5.x, which doesn't have the json module. If possible, I recommend upgrading to Python 2.7.x, as 2.5.x is badly outdated.

If you need to stick with Python 2.5.x, you'll have to use the simplejson module (see here). This code will work for 2.5.x as well as newer Python versions:

try:
    import json
except ImportError:
    import simplejson as json 

Or if you're only using Python 2.5, just do:

import simplejson as json 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advice.I installed python 2.7 and it worked
No problem at all, glad to help! :) If this resolved your problem, please click the checkbox next to the answer to accept the answer and mark the question as resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.