Twisted on Windows

Jean-Paul Calderone calderone.jeanpaul at gmail.com
Thu Nov 18 11:52:39 EST 2010


On Nov 18, 9:58 am, Bryan Richardson <btri... at gmail.com> wrote:
> Hello All,
>
> First off I must say that Twisted is a very nice event driven I/O
> package indeed. Thanks to all the developers who have contributed to
> it, as it's made my life much easier.
>
> Now for my question...
>
> I have a custom server application, and I have it structured as such:
>
> MyServerApp/ <-- root directory
>    server.py
>    foo/
>       __init__.py
>       factory.py
>
> In my server.py file, which is a Twistd config file that can be
> executed with twistd -ny server.py, I have the following import line:
>
> from foo.factory import MyServerFactory
>
> When I run 'twistd -ny server.py' on my Linux machine from within the
> root MyServerApp directory, all works as expected. However, when I try
> to do the same thing on a Windows machine using the twistd script that
> gets installed, I get an error saying that foo.factory cannot be
> found. However, if I modify my server.py file on Windows to just
> create the factory and start the reactor, I can run it just fine with
> 'python server.py' even though it still has the import line for
> MyServerFactory.
>
> Bottom line is, on Windows, the python executable can find my custom
> module in a sub directory but the twistd script cannot. Any ideas why
> this is?
>

When you run a .py file, python adds the directory containing that .py
file to the front of sys.path.  So when you run server.py,
MyServerApp/
is added to sys.path and the foo package can be found.  This happens
on
Linux and Windows.

When you run twistd, the ".py file" you're running is /usr/bin/twistd
or C:\Python26\Scripts\twistd or something else along those lines.  So
Python adds /usr/bin or C:\Python26\Scripts to sys.path.  This doesn't
help you find the foo package at all.

On Linux, when not running as root, twistd adds the current working
directory to sys.path.  So if your working directory is MyServerApp,
then the foo package can be found.

When running as root, or when running on Windows, twistd does not add
the working directory to sys.path.

So with all that in mind, the solution should be pretty clear - just
set PYTHONPATH to include MyServerApp.

This variation of twistd behavior is pretty confusing, and I think a
future version may drop the sys.path manipulation entirely, so that
it behaves consistently in all configurations.

Jean-Paul



More information about the Python-list mailing list