使用Twisted子流程和virtualenvwrapper时如何导入模块?

d_dd

我试图用Twisted编写一个Web服务器,该服务器需要用户输入并根据输入绘制图像。

对于服务器,我有一个简单的Twisted Web服务器。为了处理图像绘图,我使用python wand我正在安装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 twisted并且import 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安装,而不是我的家庭环境?

让·保罗·卡德隆(Jean-Paul Calderone)

而不是像这样运行子进程:

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

像这样运行它:

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

这会将父流程环境复制到子流程中,而不是在空环境下启动子流程。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用子流程模块时如何更改目录

来自分类Dev

使用shlex和子流程时出错

来自分类Dev

Python子流程模块和PIPE

来自分类Dev

在Python的子流程模块中使用PATH

来自分类Dev

使用权重和偏差的扫描时无法导入模块

来自分类Dev

导入CSS文件时的流程“找不到所需的模块”

来自分类Dev

如何使用子流程Popen?

来自分类Dev

使用模块时如何避免导入默认符号?

来自分类Dev

Python如何用一行导入和使用模块

来自分类Dev

如何使用virtualenv和pip从本地存储库导入模块

来自分类Dev

如何将Activiti流程定义作为子流程导入主流程?

来自分类Dev

管道和子流程模块有什么区别?

来自分类Dev

使用子流程时出现TypeError

来自分类Dev

导入模块时如何避免导入模块依赖性?

来自分类Dev

使用Python启动和停止子流程

来自分类Dev

如何使用/导入http模块?

来自分类Dev

如何使用argparse导入模块

来自分类Dev

Python子流程模块:如何重新打开PIPE?

来自分类Dev

如何为子流程模块重定向stderr

来自分类Dev

Node.js 如何导入和导出带有子功能的模块

来自分类Dev

无法在Python中使用子流程模块(无此类文件)

来自分类Dev

使用子流程模块将bash脚本转换为Python

来自分类Dev

在引用模块的导出名称时,导入如何与节点模块一起使用?

来自分类Dev

子流程popen Kivy的第二个实例给出了模块导入错误

来自分类Dev

如何在Popen子流程中使用“ >>”

来自分类Dev

如何使用多线程执行子流程

来自分类Dev

如何使用子流程更新参数(Python)

来自分类Dev

如何使用子流程写入文件

来自分类Dev

指定参数时如何导入模块?

Related 相关文章

热门标签

归档