WSGI - 导入用户创建的类
设置如下: 具有 WSGI 的 Apache 成功设置并在裸机应用程序上运行,
import sys
# Path of the directory where scripts directory is located.
sys.path.insert(0, 'C:\\Users\\xxx\\Documents\\Programming\\www\\scripts')
from Blog import Blog #Blog.py is a file in the scripts directory
def application(environ, start_response):
status = '200 OK'
output = ''
b = Blog()
for key in environ.keys():
output = output + str(key) + ' : ' + str(environ[key]) + '<br/>'
output = "Test: " + b.title + "<br/>" + b.text + "<br/>" + output
output = b.get_blog() + "<br/>" + output
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Blog.py 的相关部分看起来像
class Blog:
#blogid is a str
def get_blog(self):
conn = sqlite3.connect(self.database)
c = conn.cursor()
# get the blog
c.execute('select * from blogs')
results = []
for row in c:
results.append(row)
c.close()
return results
Apache 错误日志给我的信息:
line 21, in application
output = b.get_blog() + "<br/>" + output
AttributeError: Blog instance has no attribute 'get_blog'
将 b.get_blog 更改为 str(dir(b)) 给我: ['doc', 'init', 'module', 'get_data', 'text', 'title'] 这是一个旧的Blog 类的版本,我不久前将其包含在 wsgi 文件中。我找不到它被缓存的位置,或者为什么它没有被 Blog 导入覆盖,因为如果我将导入更改为仅导入 Blog 并将实例化为 Blog.Blog() 它仍然提供相同的目录内容。
Setup is following:
Apache with WSGI successfully set up and running on bare bones application
import sys
# Path of the directory where scripts directory is located.
sys.path.insert(0, 'C:\\Users\\xxx\\Documents\\Programming\\www\\scripts')
from Blog import Blog #Blog.py is a file in the scripts directory
def application(environ, start_response):
status = '200 OK'
output = ''
b = Blog()
for key in environ.keys():
output = output + str(key) + ' : ' + str(environ[key]) + '<br/>'
output = "Test: " + b.title + "<br/>" + b.text + "<br/>" + output
output = b.get_blog() + "<br/>" + output
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
the relevant parts of Blog.py looks like this
class Blog:
#blogid is a str
def get_blog(self):
conn = sqlite3.connect(self.database)
c = conn.cursor()
# get the blog
c.execute('select * from blogs')
results = []
for row in c:
results.append(row)
c.close()
return results
Apache error log gives me:
line 21, in application
output = b.get_blog() + "<br/>" + output
AttributeError: Blog instance has no attribute 'get_blog'
Changing the b.get_blog to str(dir(b)) gives me:
['doc', 'init', 'module', 'get_data', 'text', 'title'] which is an old version of the Blog class, one that I included in the wsgi file a while ago. I can't find where it's being cached or then why it's not being over written by the Blog import because if I change the import to just import Blog and the instantiation to Blog.Blog() it still gives the same dir contents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导入的模块具有“__file__”属性。打印出来,它会告诉您为模块加载的文件在哪里。然后你就可以解决冲突了。
可能是模块搜索路径上其他地方的版本,也可能是旧的 .pyc 文件。
Imported modules have an '__file__' attribute. Print that out, it will tell you where the file is that it loaded for the module. Then you can work out the conflict.
Maybe a version elsewhere on module search path or could be an old .pyc file.