无法导入名称 'pb'

Cannot import name 'pb'

我正在使用 Twisted 16.1.1 和 python 3.4。在 twisted 的 16.1.1 版文档中,there is a tutorial 表示“from twisted.spread import pb”。但是当我尝试导入它时,它给出了一个例外。我究竟做错了什么?

Traceback (most recent call last):
File "main.py", line 10, in <module>
from twisted.spread import pb
ImportError: cannot import name 'pb'

我正在关注 this tutorial。这是我的代码:

from twisted.internet import reactor
from twisted.spread import pb

class Echoer(pb.Root):
    def remote_echo(self, st):
        print('echoing:', st)
        return st

if __name__ == '__main__':
    reactor.listenTCP(8789, pb.PBServerFactory(Echoer()))
    reactor.run()

/usr/lib64/python3.4/site-packages/twisted/spread 上有一个名为 ui 的文件夹。没有一个叫 pb 的 folder/file。

我将 pb.py 文件复制到我的 python 文件夹,现在当我尝试导入 pb 时出现异常:

Traceback (most recent call last):
File "main.py", line 2, in <module>
from twisted.spread import pb
File "/usr/lib64/python3.4/site-packages/Twisted-16.1.1-py3.4.egg/twisted/spread/pb.py", line 890
except Error, e:
            ^
SyntaxError: invalid syntax

发生了什么事?

SyntaxError 的原因是 except Error, e: 仅在 Python 2 中有效。在 Python 3 中,它将被写入except Error as e:.

问题不在于您的代码。底层模块还没有更新到Python 3.