UnboundLocalError: local variable 'XXXX' referenced before assignment

2,477 views
Skip to first unread message

theChips

unread,
Aug 24, 2011, 8:02:36 PM8/24/11
to Google App Engine
I have no problem running my app on localhost, but when I deploy it I
receive this error:


2011-08-24 16:55:57.464 Traceback (most recent call last):
E 2011-08-24 16:55:57.464 File "/base/data/home/apps/
s~brittanybermantweets/1.352785960472444569/web/application.py", line
237, in process
E 2011-08-24 16:55:57.464 return self.handle()
E 2011-08-24 16:55:57.464 File "/base/data/home/apps/
s~brittanybermantweets/1.352785960472444569/web/application.py", line
228, in handle
E 2011-08-24 16:55:57.464 return self._delegate(fn, self.fvars, args)
E 2011-08-24 16:55:57.464 File "/base/data/home/apps/
s~brittanybermantweets/1.352785960472444569/web/application.py", line
409, in _delegate
E 2011-08-24 16:55:57.464 return handle_class(cls)
E 2011-08-24 16:55:57.464 File "/base/data/home/apps/
s~brittanybermantweets/1.352785960472444569/web/application.py", line
385, in handle_class
E 2011-08-24 16:55:57.464 return tocall(*args)
E 2011-08-24 16:55:57.464 File "/base/data/home/apps/
s~brittanybermantweets/1.352785960472444569/brittanyberman.py", line
18, in GET
E 2011-08-24 16:55:57.464 tweets = brittanytweets.getTweets()
E 2011-08-24 16:55:57.464 File "/base/data/home/apps/
s~brittanybermantweets/1.352785960472444569/brittanytweets.py", line
16, in getTweets
E 2011-08-24 16:55:57.464 tweets = json.loads(rawtweets)
E 2011-08-24 16:55:57.464 UnboundLocalError: local variable
'rawtweets' referenced before assignment


To make matters more confusing, it worked fine when I deployed it last
night.

Here is the python in question:

import urllib2
import simplejson as json

url = "http://api.twitter.com/1/statuses/user_timeline.json?
screen_name=brittanyberman"


def getTweets():
try:
rawtweets = urllib2.urlopen(url).read()
except urllib2.HTTPError, e:
print "HTTP error: %d" % e.code
except urllib2.URLError, e:
print "Network error: %s" % e.reason.args[1]

alltweets=[]
tweets = json.loads(rawtweets)

for i in range(12):
alltweets.append(tweets[i]['text'])

#strip out the 'u' char
for i in range(len(alltweets)):
alltweets[i]=str(alltweets[i])

return alltweets


The script runs fine on a local shell. Any idea what's wrong?

Thanks!

Robert Schuppenies

unread,
Aug 26, 2011, 11:14:45 AM8/26/11
to google-a...@googlegroups.com
Looks to me like your "urllib2.urlopen(url).read()" call fails. UnboundLocalError means that 'rawtweets' wasn't assigned, and it is only assigned if this call returns successfully.

Here is another example for this:
"""
>>> def raise_error():
...   raise ValueError("i'm an error")
... 
>>> def look_ma_referenced_before_assignment():
...   try:
...     x = raise_error()
...   except ValueError, e:
...     print e
...   print x
... 
>>> look_ma_referenced_before_assignment()
i'm an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in look_ma_referenced_before_assignment
UnboundLocalError: local variable 'x' referenced before assignment
"""

So I would check if "urllib2.urlopen(url).read()" actually works. For example:
- check if an error message is printed
- catch all errors, not just urllib2 errors.

cheers,
robert


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.


Reply all
Reply to author
Forward
0 new messages