我刚刚开始使用twisted.web,并且在将Python 模块导入.rpy
脚本时遇到问题。
在 C:\py\twisted\mysite.py
中,我有这个:
from twisted.web.resource import Resource
from twisted.web import server
class MySite(Resource):
def render_GET(self, request):
request.write("<!DOCTYPE html>")
request.write("<html><head>")
request.write("<title>Twisted Driven Site</title>")
request.write("</head><body>")
request.write("<h1>Twisted Driven Website</h1>")
request.write("<p>Prepath: <pre>{0}</pre></p>".format(request.prepath))
request.write("</body></html>")
request.finish()
return server.NOT_DONE_YET
在 C:\py\twisted\index.rpy
中,我有这个:
import mysite
reload(mysite)
resource = mysite.MySite()
我跑了 < code>twistd -n web --port 8888 --path C:\py\twisted 在命令提示符下,服务器成功启动。但是当我请求 localhost:8888 时,我得到了一个源自 ImportError 的(巨大的)堆栈跟踪:
: No module named mysite
I可以从解释器导入模块,如果我只是将 index.rpy
作为 python 脚本执行,我不会收到导入错误。关于这个主题的文档有点模糊,它只是说“但是,在 Python 模块中定义资源子类通常是一个更好的主意。为了使模块中的更改可见,您必须重新启动 Python 进程,或者重新加载模块:”(来自此处)。
有谁知道执行此操作的正确方法?
I'm just getting started with twisted.web, and I'm having trouble importing a Python module into a .rpy
script.
in C:\py\twisted\mysite.py
, I have this:
from twisted.web.resource import Resource
from twisted.web import server
class MySite(Resource):
def render_GET(self, request):
request.write("<!DOCTYPE html>")
request.write("<html><head>")
request.write("<title>Twisted Driven Site</title>")
request.write("</head><body>")
request.write("<h1>Twisted Driven Website</h1>")
request.write("<p>Prepath: <pre>{0}</pre></p>".format(request.prepath))
request.write("</body></html>")
request.finish()
return server.NOT_DONE_YET
and in C:\py\twisted\index.rpy
, I have this:
import mysite
reload(mysite)
resource = mysite.MySite()
I ran twistd -n web --port 8888 --path C:\py\twisted
in command prompt and the server started successfully. But when I requested localhost:8888
I got a (huge) stack trace originating from an ImportError:
<type 'exceptions.ImportError'>: No module named mysite
I can import the module from the interpreter, and if i just execute index.rpy
as a python script, I don't get the import error. The documentation on this subject is a bit vague, it just says "However, it is often a better idea to define Resource subclasses in Python modules. In order for changes in modules to be visible, you must either restart the Python process, or reload the module:" (from here).
Does anyone know the proper way to do this?
发布评论
评论(1)
简短回答:您需要设置 PYTHONPATH 以包含
C:\py\twisted
。很长的答案...
rpy 脚本基本上只是一些 Python 代码,就像任何其他 Python 代码一样。因此,rpy 脚本中的导入就像任何其他 Python 代码中的导入一样。对于最常见的情况,这意味着按顺序一个接一个地访问 sys.path 中的目录,并且如果找到与导入名称匹配的 .py 文件,该文件用于定义模块。
sys.path
主要由静态定义填充,包括 C:\Python26\Lib\ 和PYTHONPATH
环境变量。然而,还有一件事值得了解。当您运行“python”时,当前工作目录将添加到sys.path
的前面。当你运行“python C:\foo\bar\baz.py”时,C:\foo\bar\'会被添加到
sys.path的前面。但是,当您运行“twistd ...”时,不会向 sys.path 添加任何有用的内容。
最后一个行为可能解释了为什么如果您直接运行 rpy 脚本,或者如果您运行 python 并尝试以交互方式导入模块,您的测试会起作用,但在使用 twind 时会失败。将
C:\py\twisted
添加到PYTHONPATH
环境变量应该使得当 rpy 脚本从使用 twind 启动的服务器运行时该模块可导入。Short answer: you need to set PYTHONPATH to include
C:\py\twisted
.Long answer...
An rpy script is basically just some Python code, like any other Python code. So an import in a rpy script works just like an import in any other Python code. For the most common case, this means that the directories in
sys.path
are visited one by one, in order, and if a.py
file matching the imported name is found, that file is used to define the module.sys.path
is mostly populated from a static definition including things like C:\Python26\Lib\ and from thePYTHONPATH
environment variable. However, there's one extra thing worth knowing about. When you run "python", the current working directory is added to the front ofsys.path
. When you run "python C:\foo\bar\baz.py",C:\foo\bar\' is added to the front of
sys.path. But when you run "twistd ...", nothing useful is added to
sys.path`.This last behavior probably explains why your tests work if you run the rpy script directly, or if you run python and try to import the module interactively, but fail when you use twistd. Adding
C:\py\twisted
to thePYTHONPATH
environment variable should make the module importable when the rpy script is run from the server you start with twistd.