Can't import my module when start my twisted application under root

Posted by kepkin on Stack Overflow See other posts from Stack Overflow or by kepkin
Published on 2010-03-03T18:33:20Z Indexed on 2010/04/03 18:03 UTC
Read the original article Hit count: 341

Filed under:
|
|

Here is absolutely minimal application so you could try to reproduce it on your machine. Having two files for example in /home/aln/tmp/tw_test:

  • server.tac
  • MyLib.py

MyLib.py

class Solver(object):
    def solve(self):
        """ do extremely complex stuff here """
        print "Hello from solve"

server.tac

#!/usr/bin/python
import MyLib

from twisted.application import internet, service
from twisted.internet import protocol, reactor, defer, utils, threads
from twisted.protocols import basic

class MyProtocol(basic.LineReceiver):

    def lineReceived(self, line):
        if line=="new job":
            self.transport.write("started a job" + '\r\n')
            self.factory.run_defered()

class MyFactory(protocol.ServerFactory, MyLib.Solver):
    protocol = MyProtocol

    def run_defered_helper(self):
        self.solve()

    def run_defered(self):
        d = threads.deferToThread(self.run_defered_helper)

application = service.Application('MyApplication')
factory = MyFactory()
internet.TCPServer(1079, factory).setServiceParent(service.IServiceCollection(application))

Everything works fine when I start it under non-root user.

aln@aln-laptop:tw_test$ twistd -ny server.tac 
2010-03-03 22:42:55+0300 [-] Log opened.
2010-03-03 22:42:55+0300 [-] twistd 8.2.0 (/usr/bin/python 2.6.4) starting up.
2010-03-03 22:42:55+0300 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2010-03-03 22:42:55+0300 [-] <class 'MyFactory'> starting on 1079
2010-03-03 22:42:55+0300 [-] Starting factory <MyFactory object at 0x2d5ea50>
2010-03-03 22:42:59+0300 [MyProtocol,0,127.0.0.1] Hello from solve
^C2010-03-03 22:43:01+0300 [-] Received SIGINT, shutting down.
2010-03-03 22:43:01+0300 [-] (Port 1079 Closed)
2010-03-03 22:43:01+0300 [-] Stopping factory <MyFactory object at 0x2d5ea50>
2010-03-03 22:43:01+0300 [-] Main loop terminated.
2010-03-03 22:43:02+0300 [-] Server Shut Down.

But if try to start it under root (which is going to happen in my real application) I receive the following exception:

aln@aln-laptop:tw_test$ sudo twistd -ny server.tac 
[sudo] password for aln: 
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 694, in run
    runApp(config)
  File "/usr/lib/python2.6/dist-packages/twisted/scripts/twistd.py", line 23, in runApp
    _SomeApplicationRunner(config).run()
  File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 411, in run
    self.application = self.createOrGetApplication()
  File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 494, in createOrGetApplication
    application = getApplication(self.config, passphrase)
--- <exception caught here> ---
  File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 505, in getApplication
    application = service.loadApplication(filename, style, passphrase)
  File "/usr/lib/python2.6/dist-packages/twisted/application/service.py", line 390, in loadApplication
    application = sob.loadValueFromFile(filename, 'application', passphrase)
  File "/usr/lib/python2.6/dist-packages/twisted/persisted/sob.py", line 215, in loadValueFromFile
    exec fileObj in d, d
  File "server.tac", line 2, in <module>
    import MyLib
exceptions.ImportError: No module named MyLib

Failed to load application: No module named MyLib

If I try to load MyLib module in the python intepreter under root, it works fine:

aln@aln-laptop:tw_test$ sudo python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MyLib
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages']
>>> 

sys.path is absolutely the same for aln user. I tried sudo -E too. Any suggestions?

© Stack Overflow or respective owner

Related posts about twisted

Related posts about linux