簡體   English   中英

使用Twisted子進程和virtualenvwrapper時如何導入模塊?

[英]How do I import modules when using Twisted subprocesses and virtualenvwrapper?

我試圖在Twisted中編寫一個Web服務器,它接受用戶輸入並根據輸入繪制圖像。

對於服務器,我有一個簡單的Twisted Web服務器。 為了處理圖像繪制,我使用的是python 魔杖 我正在使用裝有Twisted和Wand的virtualenvwrapper工作。 但是,當我運行服務器時,我收到導入錯誤:

Traceback (most recent call last):
  File "insert_bubble.py", line 1, in <module>
    from wand.image import Image
ImportError: No module named wand.image

如果我去python解釋器,我可以import twistedimport wand.image罰款。 我懷疑子流程沒有使用正確的環境。 解決方法是將子進程使用的所有模塊安裝到我的用戶帳戶,但我想避免這種情況。

服務器代碼主要來自Twisted教程頁面。

import sys
from twisted.internet import protocol
from twisted.internet import reactor
import re

class MyPP(protocol.ProcessProtocol):
    def __init__(self, verses):
        self.verses = verses
        self.data = ""
    def connectionMade(self):
        print "connectionMade!"
        self.transport.closeStdin() # tell them we're done
    def outReceived(self, data):
        print "outReceived! with %d bytes!" % len(data)
        self.data = self.data + data
    def errReceived(self, data):
        print "errReceived! with %d bytes!" % len(data)
        self.data= self.data+data
    def inConnectionLost(self):
        print "inConnectionLost! stdin is closed! (we probably did it)"
    def outConnectionLost(self):
        print "outConnectionLost! The child closed their stdout!"
        # now is the time to examine what they wrote
        print "I saw them write:", self.data
        #(dummy, lines, words, chars, file) = re.split(r'\s+', self.data)
        #print "I saw %s lines" % lines
    def errConnectionLost(self):
        print "errConnectionLost! The child closed their stderr."
    def processExited(self, reason):
        print "processExited, status %d" % (reason.value.exitCode,)
    def processEnded(self, reason):
        print "processEnded, status %d" % (reason.value.exitCode,)
        print "quitting"
        reactor.stop()

pp = MyPP(10)
reactor.spawnProcess(pp, sys.executable, ['python', 'insert_bubble.py'], {})
reactor.run()

如何讓子進程使用正確的virtualenv Python安裝,而不是我的家庭環境?

而不是像這樣運行子進程:

reactor.spawnProcess(
    pp, sys.executable, ['python', 'insert_bubble.py'], {}
)

像這樣運行:

reactor.spawnProcess(
    pp, sys.executable, ['python', 'insert_bubble.py'], os.environ
)

這會將父進程環境復制到子進程中,而不是使用空環境啟動子進程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM