Twisted on Windows

B

Bryan Richardson

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?
 
J

Jean-Paul Calderone

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top