0

这是我的 index.py(它在文件夹 /NetWeave_Custom 中)

import web
import lib.html

web.config.debug = True

urls = (
    '/', 'index',
)

class index:
    def GET(self):
        markup = html.abstr()
        print markup.element

if __name__ == "__main__":
    app.run()

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

然后这是我的 html.py(它在 /NetWeave_Custom/lib/ 中)

class abstr:
    element = 'Hello World';

但是我收到 500 内部服务器错误。谁能告诉我我做错了什么?我是 web.py 框架的新手。谢谢!

编辑:

使用上面的代码,我得到了错误: ImportError: no module named lib.html

最终编辑:

工作代码如下所示:

import web
from lib import html

web.config.debug = True

urls = (
    '/', 'index',
)

class index:
    def GET(self):
        markup = html.abstr()
        return markup.element

if __name__ == "__main__":
    app = web.application(urls, globals(), autoreload=False)
    app.run()

application = app.wsgifunc()

然后这是我的 html.py(它在 /NetWeave_Custom/lib/ 中)

class abstr:
    element = 'Hello World';

浏览器显示:'Hello World' 所以更改是在调用它之前定义应用程序(不是真正相关,但对你来说这是必要的——没有这个它确实可以正常工作),返回 markup.element 而不是打印它,并创建一个lib 子目录中的空白__init__.py文件,因此据我所知, lib 将被视为模块(或包?)。

谢谢!

4

2 回答 2

1

您 import lib.html,但不要使用该全名。相反,您仅指html.

如果出现导入​​错误,则说明lib找不到包;可能有以下两种情况之一:

  • 您应该import html改用,这也可以解决不正确的参考。

  • lib目录缺少它的__init__.py文件(可以为空)。该文件将使其成为一个包,并允许您导入它。然后将引用更改htmllib.html

    class index:
        def GET(self):
            markup = lib.html.abstr()
            print markup.element
    

    将导入更改为:

    from lib import html
    

您还尝试app在定义之前运行。将最后几行更改为:

if __name__ == "__main__":
    app = web.application(urls, globals(), autoreload=False)
    app.run()

最后但同样重要的是,您需要将NetWeave_Custom目录(完整、绝对路径)添加到PYTHONPATH; 如果您正在使用mod_wsgi查看WSGIPythonPath指令。

于 2012-12-27T17:21:06.000 回答
0

答案是双重的。__init__.py我在 lib 子目录中没有文件。另一个错误是我写的print,而不是return markup.element……Doh!

于 2012-12-28T20:45:06.097 回答