Here are my imports:
from bottle import request, route, run, template, static_file, redirect
from urllib2 import urlopen, URLError, Request
from pymongo import MongoClient
from config.development import config
import json
And here is the offending line (and another line that I think may be causing an issue):
game_id = request.forms.get('game_id')
request = Request(config['singlegame_url'].replace('$GAMEID', game_id))
The error I'm getting is:
UnboundLocalError("local variable 'request' referenced before assignment",)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "app.py", line 23, in add
game_id = request.forms.get('game_id')
UnboundLocalError: local variable 'request' referenced before assignment
My first thought was that the two request
modules were causing issues, but I couldn't get the error to go away by messing around with the imports and importing stuff as
another name.
You must rename your request
variable to something else.
Python kind of reserves the variable name request
to be a local variable due to request = ...
before actually executing the code.
The interpreter then executes your line game_id = request.forms.get('game_id')
, where request
is the new reserved local variable, which is, undefined.
Here's a good example of the same issue:
>>> x = 1
>>> def f():
... print(x) # You'd think this prints 1, but `x` is the local variable now
... x = 3
>>> f()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
f()
File "<pyshell#4>", line 2, in f
print(x)
UnboundLocalError: local variable 'x' referenced before assignment