python - 无法导入名称 'pb'

标签 python python-3.x twisted

我使用的是 Twisted 16.1.1 和 python 3.4。在 twisted 的 16.1.1 版文档中,there is a tutorial上面写着“from twisted.spread import pb”。但是当我尝试导入它时,它给出了一个异常(exception)。我究竟做错了什么?

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 的文件夹/文件。

我将 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。

关于python - 无法导入名称 'pb',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176437/

相关文章:

python - Pandas - 按日期计算不同的值 - 更有效的方法?

Python + Cronjob : Why doesn’t cronjob execute os. 系统()?

python - 播放从 url 检索到的声音内容?

python - 在 NumPy 中,如何在不使用 np.s_ 的情况下从一维数组中提取范围?

python - Twisted:在 connectTCP 之后关闭连接而不丢失其他连接

python - 如何在交互式 python 环境中导入 Odoo/OpenERP Addon 模块?

python - 如何使用 pd.Grouper 按月对日期进行分组?

iphone - 通过 Twisted 服务器进行 Apple 商店收据验证

Python装饰器内部调用了错误的函数

python - Django 获取相关对象的多对多关系