开发者

import problem with twisted.web server

开发者 https://www.devze.com 2022-12-30 22:04 出处:网络
I\'m just getting started with twisted.web, and I\'m having trouble importing a Python module into a .rpy script.

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?


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 the PYTHONPATH environment variable. However, there's one extra thing worth knowing about. When you run "python", the current working directory is added to the front of sys.path. When you run "python C:\foo\bar\baz.py", C:\foo\bar\' is added to the front ofsys.path. But when you run "twistd ...", nothing useful is added tosys.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 the PYTHONPATH environment variable should make the module importable when the rpy script is run from the server you start with twistd.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号