在root下启动我的扭曲应用程序时无法导入我的模块

时间:2010-03-03 18:33:21

标签: python linux twisted

  

可能重复:
  Running twistd as root, modules aren't found

这绝对是最小的应用程序,因此您可以尝试在您的计算机上重现它。 在/ 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))

当我在非root用户下启动时,一切正常。

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.

但是如果尝试在root下启动它(这将在我的实际应用程序中发生),我会收到以下异常:

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

如果我尝试在root下的python intepreter中加载MyLib模块,它可以正常工作:

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']
>>> 
对于aln用户,

sys.path绝对相同。我也试过sudo -E。 有什么建议吗?

0 个答案:

没有答案