WSGI - 导入用户创建的类

发布于 2024-11-27 06:42:08 字数 1619 浏览 1 评论 0原文

设置如下: 具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-12-04 06:42:08

导入的模块具有“__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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文