Webpy显示pyc文件路径而不是print语句

时间:2015-09-17 17:51:28

标签: python python-2.7 web.py web-frameworks

我确信我错过了教程中非常明显的内容,但尝试通过实现一个简单的脚本和html页面来学习webpy框架。

这是我从命令行运行以启动“服务器”的主要脚本(我在Index类工作/设置中没有正确的POST功能,但只是尝试使用GET功能正常工作以开头):

import web
import sys
sys.path.insert(0, '/Users/seanjarp/PythonLearnings/projects/GAME/ChipChippersonGame') 
from ChipChippersonGame import ChipChippersonGame

urls = (
    '/testhtml', 'Index'
) 

app = web.application(urls, globals())
render = web.template.render('templates/')

class Index(object):
    def GET(self):
        return render.testhtml(ChipChippersonGame)
       # return render.testhtml()
    #def POST(self):
    #    form = web.input(playerinput="HiChip")
    #    user_response = ChipChippersonGame.next_scene(form)


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

上面这个脚本是在另一个导演中导入另一个脚本,目前我已经简化为只有一个print语句来保持简单(导入工作正常):

print "ChipChippersonGame script is being called"

testhtml文件如下所示:

$def with (ChipChippersonGame)

<html>
    <head>
        <title>What's Up!</title>
    </head>
<body>

$if ChipChippersonGame:
    $ChipChippersonGame

$else:
   <em>Script is not being called!</>

</body>
</html>

显然,当我将这个例子加载到我的浏览器中时:

http://0.0.0.0:4141/testhtml

它将以下内容输出到浏览器:

<module 'ChipChippersonGame.ChipChippersonGame' from '/Users/seanjarp/PythonLearnings/projects/GAME/ChipChippersonGame/ChipChippersonGame`/ChipChippersonGame.pyc

我做错了什么阻止了ChipChippersonGame.py脚本中的print语句打印到浏览器?

我假设我已经搞砸了我在testhtml.html文件中调用或设置函数的方式。

1 个答案:

答案 0 :(得分:2)

您的初始模块导入会产生打印到标准输出的副作用,因此在您启动应用程序时可能会将其写入日志。

其次,您将module对象传递给模板,然后直接在标记中输出,这就是打印出模块__repr__的原因。

$if ChipChippersonGame:
    $ChipChippersonGame
    ^__________________this is a module object hence <module 'ChipChippersonGame.ChipChippersonGame' from
相关问题