<type 'exceptions.NameError'>: 全局名称“..”未定义
<type 'exceptions.NameError'>: global name '..' is not defined
我正在创建一个 python 网络界面。您完成表单,它会将其发送到后台 python 函数,事情就会发生。
我在使用其中一个后台函数时遇到了问题,这让我很困惑,因为这不是我第一次创建这样的快速项目,而且我以前也没有遇到过这个错误。
我有三个文件:commands.py、header.py 和 product.py
product.py 看起来像这样:
#!/usr/bin/python
import sys
import os
import cgitb
cgitb.enable()
sys.path.append(os.path.abspath("/var/www/cgi-bin/includes/"))
from header import *
page = get_page_name(sys.argv[0])
printDefaultPage(0,page)
这是它通过header.py:
的路线
def printDefaultPage(error,page): #this prints the page
printHTTPHeaders() #print headers
printHeader(page) #print header.inc
printPage(page)
printFooter() #print footer.inc
上述函数通过调用其他函数打印出页眉、页眉、页体和页脚。
def printPage(page):
if page == "product":
print """<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>"""
list_subscriptions()
print """<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>"""
如您所见,printPage 调用了一个函数 list_subscriptions(),它看起来有点像这样:
def list_subscriptions():
subscriptions=get_subscriptions()
print """<select name = "sub">"""
for s in subscriptions:
id = s
price = subscriptions.get(str(s)).get('price')
scans = subscriptions.get(str(s)).get('scans_per_month')
print """<option value ="""+str(id)+"""">"""+str(price)+""" - """+str(scans)+"""</option>"""
print "</select>"
这又会调用 get_subscriptions,它存在于名为命令的文件中,该文件在顶部导入:
#!/usr/bin/python
from commands import *
为了完整起见,这里是 get_subscriptions:
def get_subscriptions():
subscriptions=proxy.get_subscriptions()
return subscriptions
另一端:
def get_subscriptions(self):
subscriptions=self.store.find(Subscription)
subs = {}
for sub in subscriptions:
subs[str(sub.id)] = {}
subs[str(sub.id)]['scans_per_month'] = sub.scans_per_month
subs[str(sub.id)]['price'] = int(sub.price)
return subs
get_subscriptions() returns 字典。
更令人困惑的是,当我在 python 命令行中 运行 printPage("product") 时,它起作用了。
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from header import *
>>> printPage("product")
<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>
然而,当我在浏览器中转到 product.py 并期望看到一个漂亮的 select 框时,我却收到了这个严重错误:
/var/www/cgi-bin/includes/header.py in list_subscriptions()
7
8 def list_subscriptions():
=> 9 subscriptions=get_subscriptions()
10 print """<select name = "sub">"""
11 for s in subscriptions:
subscriptions undefined, get_subscriptions undefined
编辑:所有文件都chmod到755,并且都归www-data所有。
编辑:在python 命令行 printDefaultPage(0,"product") returns this:
>>> printDefaultPage(0,"product")
Content-Type: text/html
Vary: *
Title: Encriptor
Cache-Control: no-cache, no store, must-revalidate
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Encriptor Scanner</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="encription.co.uk" />
<meta name="language" content="en" />
<link rel="shortcut icon" href="/css/images/favicon.ico" />
<link href="/css/style.css" rel="stylesheet" type="text/css" />
<body>
<div class="wrap"
><div id="header">
<p id="logname"></p>
<img src="/images/raptor-logo.png" border="0"
id="logo-main"
/></div>
<ul class="menu">
</ul>
<div id="content">
<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>
</div>
<p id="spacer"> </p>
</div>
</div>
</br>
<center><div id="footerlog">Encryptor v3 <span id="dot"$bull;</span>Copyright © 2015 <a href="https://www.encription.co.uk" target="_ne$
</div></center>
</body>
</html>
符合预期,但在浏览器中无法正常工作。
我找到问题了!
放弃这个我开始在另一个页面上工作,但是很快同样的问题开始浮出水面。
commands.py 已被 python 使用,这意味着我一直在导入错误的模块。
糟糕!
我正在创建一个 python 网络界面。您完成表单,它会将其发送到后台 python 函数,事情就会发生。
我在使用其中一个后台函数时遇到了问题,这让我很困惑,因为这不是我第一次创建这样的快速项目,而且我以前也没有遇到过这个错误。
我有三个文件:commands.py、header.py 和 product.py
product.py 看起来像这样:
#!/usr/bin/python
import sys
import os
import cgitb
cgitb.enable()
sys.path.append(os.path.abspath("/var/www/cgi-bin/includes/"))
from header import *
page = get_page_name(sys.argv[0])
printDefaultPage(0,page)
这是它通过header.py:
的路线def printDefaultPage(error,page): #this prints the page
printHTTPHeaders() #print headers
printHeader(page) #print header.inc
printPage(page)
printFooter() #print footer.inc
上述函数通过调用其他函数打印出页眉、页眉、页体和页脚。
def printPage(page):
if page == "product":
print """<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>"""
list_subscriptions()
print """<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>"""
如您所见,printPage 调用了一个函数 list_subscriptions(),它看起来有点像这样:
def list_subscriptions():
subscriptions=get_subscriptions()
print """<select name = "sub">"""
for s in subscriptions:
id = s
price = subscriptions.get(str(s)).get('price')
scans = subscriptions.get(str(s)).get('scans_per_month')
print """<option value ="""+str(id)+"""">"""+str(price)+""" - """+str(scans)+"""</option>"""
print "</select>"
这又会调用 get_subscriptions,它存在于名为命令的文件中,该文件在顶部导入:
#!/usr/bin/python
from commands import *
为了完整起见,这里是 get_subscriptions:
def get_subscriptions():
subscriptions=proxy.get_subscriptions()
return subscriptions
另一端:
def get_subscriptions(self):
subscriptions=self.store.find(Subscription)
subs = {}
for sub in subscriptions:
subs[str(sub.id)] = {}
subs[str(sub.id)]['scans_per_month'] = sub.scans_per_month
subs[str(sub.id)]['price'] = int(sub.price)
return subs
get_subscriptions() returns 字典。 更令人困惑的是,当我在 python 命令行中 运行 printPage("product") 时,它起作用了。
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from header import *
>>> printPage("product")
<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>
然而,当我在浏览器中转到 product.py 并期望看到一个漂亮的 select 框时,我却收到了这个严重错误:
/var/www/cgi-bin/includes/header.py in list_subscriptions()
7
8 def list_subscriptions():
=> 9 subscriptions=get_subscriptions()
10 print """<select name = "sub">"""
11 for s in subscriptions:
subscriptions undefined, get_subscriptions undefined
编辑:所有文件都chmod到755,并且都归www-data所有。
编辑:在python 命令行 printDefaultPage(0,"product") returns this:
>>> printDefaultPage(0,"product")
Content-Type: text/html
Vary: *
Title: Encriptor
Cache-Control: no-cache, no store, must-revalidate
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Encriptor Scanner</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="encription.co.uk" />
<meta name="language" content="en" />
<link rel="shortcut icon" href="/css/images/favicon.ico" />
<link href="/css/style.css" rel="stylesheet" type="text/css" />
<body>
<div class="wrap"
><div id="header">
<p id="logname"></p>
<img src="/images/raptor-logo.png" border="0"
id="logo-main"
/></div>
<ul class="menu">
</ul>
<div id="content">
<form method='post' id="register_form" name="client_frm"></br>
<label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
</form>
</div>
<p id="spacer"> </p>
</div>
</div>
</br>
<center><div id="footerlog">Encryptor v3 <span id="dot"$bull;</span>Copyright © 2015 <a href="https://www.encription.co.uk" target="_ne$
</div></center>
</body>
</html>
符合预期,但在浏览器中无法正常工作。
我找到问题了! 放弃这个我开始在另一个页面上工作,但是很快同样的问题开始浮出水面。
commands.py 已被 python 使用,这意味着我一直在导入错误的模块。 糟糕!