不确定如何解决此瓶子错误(Python)
以下是我的作品:不确定如何解决此瓶子错误(Python),python,import,bottle,Python,Import,Bottle,以下是我的作品: 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 这是令人不快的一句话(我认为另一句话可能会引起问题): 我得到的错误是: Un
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
这是令人不快的一句话(我认为另一句话可能会引起问题):
我得到的错误是:
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
我的第一个想法是两个
请求
模块引起了问题,但我无法通过混淆导入并将导入为另一个名称来消除错误。必须将请求
变量重命名为其他名称
Python在实际执行代码之前,保留变量名request
为局部变量,因为request=…
。
解释器然后执行您的行game\u id=request.forms.get('game\u id')
,其中request
是新的保留局部变量,未定义
下面是一个很好的例子:
>>> 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
>x=1
>>>def():
... print(x)#您可能会认为它打印1,但现在'x'是局部变量
... x=3
>>>f()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
f()
文件“”,第2行,在f中
打印(x)
UnboundLocalError:赋值前引用了局部变量“x”
我想我还必须更改其中一个请求模块的名称?我有一个来自battle
的,还有一个来自urllib2
@maxmakie-Python是区分大小写的,因此Request
与Request
不同。因此,您不必更改名称:)
>>> 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