GAE AttributeError
My 开发者_如何学GoGAE app runs fine from my computer, but when I upload it, I start getting an AttributeError, specifically:
AttributeError: 'dict' object has no attribute 'item'
I am using the pylast interface (an API for last.fm--link). Specifically, I am accessing a list of variables of this type:
SimilarItem = _namedtuple("SimilarItem", ["item", "match"])
I have a variable of this type, call it sim
, and I am trying to access sim.item
when I get the attribute error.
I should note that I am using Python 2.6 on my computer, and I understand that GAE runs on Python 2.5. Would that make a difference here? I thought they were backwards-compatible.
Lastly, I think it could be a possible problem with the modules that pylast
imports--maybe they don't work with GAE or something? I did some research but I didn't get any results. Here are the imports:
import hashlib
import httplib
import urllib
import threading
from xml.dom import minidom
import xml.dom
import time
import shelve
import tempfile
import sys
import htmlentitydefs
I would appreciate any help with this frustrating issue. Thanks in advance.
Yep, Python 2.6 is mostly backwards-compatible to 2.5 -- this means that what runs in 2.5 will mostly run in 2.6. But you seem to misunderstand what backwards means -- it's the antonym of forwards, meaning that it's perfectly possible that what runs in 2.6 (if it uses new features that are in 2.6 but were not in 2.5) will not run in 2.5 (or previous versions). Surely you see that the only way to avoid that would be to never, ever add any new features in new versions of Python -- a pretty dire remedy!-)
So I don't think you're well advised to use 2.6 for your local GAE development, since you know that 2.5 is what the production version will be using. Why not download and install a Python 2.5 from python.org (or your linux distro's specific repos, if you're on Linux) and point your GAE SDK to use that version, instead of 2.6? That would make your life substantially easier!
Python 2.5 doesn't have namedtuple
. It was added in 2.6.
Edit: It looks like the _namedtuple
function is provided by pylast
and will use a real namedtuple
in Python 2.6+ but will fall back to a plain old dict
in Python 2.5 or older. That means that you'll have to use sim['item']
or sim.get('item')
when running in production.
Here's the source for that function:
def _namedtuple(name, children):
"""
collections.namedtuple is available in (python >= 2.6)
"""
v = sys.version_info
if v[1] >= 6 and v[0] < 3:
return collections.namedtuple(name, children)
else:
def fancydict(*args):
d = {}
i = 0
for child in children:
d[child.strip()] = args[i]
i += 1
return d
return fancydict
精彩评论