Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import name 'pb'

I'm using Twisted 16.1.1 and python 3.4. On twisted's documentation for version 16.1.1, there is a tutorial that says `from twisted.spread import pb'. But when I try to import it it gives an exception. What am I doing wrong?

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

I'm follwing this tutorial. This is my code:

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()

On /usr/lib64/python3.4/site-packages/twisted/spread there is on one folder named ui. There is not a folder/file called pb.

I copied the pb.py file to my python folder, now when I try to import pb I get an Exception:

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

What is happening?

like image 541
Carlos Porta Avatar asked Nov 08 '22 15:11

Carlos Porta


1 Answers

The reason for the SyntaxError is that except Error, e: in only valid in Python 2. In Python 3, it would be written except Error as e:.

The problem isn't with your code. The underlying module hasn't yet been updated to Python 3.

like image 158
Raymond Hettinger Avatar answered Nov 15 '22 11:11

Raymond Hettinger