Webpy:源代码更改时如何自动重新加载?
我正在学习 webpy 来编写代码。 我正在使用 Webpy 框架 0.34。我的操作系统是ubuntu 11.04,python的版本是2.7。 在 webpy.org 的 教程 中说:
“当使用内置网络服务器运行时,它会启动应用程序 在调试模式下。在调试模式下,对代码和模板的任何更改都会 自动重新加载”
但它不起作用。
我的code.py与教程的示例相同:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
class index():
def GET(self):
return 'a'
if __name__ == '__main__':
urls = ('/', 'index')
app = web.application(urls, globals())
app.run()
我正在使用内置网络服务器(用于
python code.py
启动服务器)
并且当我更改代码时, “return 'a'”改为“return 'b'”,在浏览器中,它一直显示'a',直到我
每次重写代码都重新启动服务器,这是浪费时间,为什么自动重载机制不起作用?我是吗做错了什么?谢谢您的帮助。
编辑:抱歉,我的 code.py 与教程的示例不完全相同,如果我将 url 分配行放在 if 之外,则会导致问题。声明,重新加载机制有效!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
class index():
def GET(self):
return 'a'
urls = ('/', 'index')
if __name__ == '__main__':
app = web.application(urls, globals())
app.run()
但我不知道为什么......
I'm learning webpy to write code.
I'm using Webpy framework 0.34. my operating system is ubuntu 11.04 and python's version is 2.7.
in webpy.org 's tutorials it saids:
"When running with the built-in webserver, it starts the application
in debug mode. In debug mode any changes to code and templates are
automatically reloaded"
but it doesn't work.
my code.py is the same as the tutorial's sample:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
class index():
def GET(self):
return 'a'
if __name__ == '__main__':
urls = ('/', 'index')
app = web.application(urls, globals())
app.run()
and I am using the build-in webserver(used
python code.py
to start the server)
when I change the code "return 'a'" to "return 'b'", in web browser, it keeps displaying 'a', untill I restart the server.
restart server every time you rewrite the code is wasting time, why autoreload mechanism does not work? Am I doing something wrong? thank you for any help.
edit: sorry, my code.py is not exactly the same as the tutorial's sample, that cause the problem. if I put the urls assignment line outside the if statement, the reload mechanism works!!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
class index():
def GET(self):
return 'a'
urls = ('/', 'index')
if __name__ == '__main__':
app = web.application(urls, globals())
app.run()
but I don't know why...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您像我一样有单独的
route.py
文件,请不要像这样设计文件:它不会在文档更改时重新加载,而是这样做:
if you have separate
route.py
file like me, don't design the file like this:it won't reload on document change, do this instead :
我只有一个想法:您的编辑器不会更改文件的修改时间。尝试通过在代码修改后运行以下命令来强制更改时间戳,然后检查结果:
I have only one idea: your editor doesn't changing time of modification of your files. Try to enforce changing timestamp by running the following command after code modification and after that check the results:
尝试将以下内容添加到代码顶部(导入后):
有关详细信息:http:// webpy.org/docs/0.3/tutorial#developmenting
Try adding the following to the top of your code (after imports):
For more info: http://webpy.org/docs/0.3/tutorial#developing
您应该在更改并保存代码后刷新页面
you should refresh the page after you change and save the code