Search code examples
pythontwistedtcpserver

Python twisted reactor undefined variable


I'm following this tutorial: http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

but when I write the

reactor.listenTCP(80, factory)

eclipse tells me that it's an undefined variable.. I installed twisted and can get autocomplete for the import, but this won't work.. Google showed a few more having this issue but I couldn't find any solution to it.. Thanks!

EDIT: The complete code:

from twisted.internet.protocol import Factory
from twisted.internet import reactor

factory = Factory()
reactor.listenTCP(80, factory)
reactor.run()

Solution

  • This is a known issue related to the way Eclipse/PyDev performs static analysis.

    If you look closely, the reactor object does not actually exist in the twisted.internet module at import-time. The module is empty.

    When Eclipse/PyDev tries to compile the bytecode, static analysis doesn't see the reactor object in the twisted.internet module and marks it as an undefined variable, even though it's actually present at runtime (registered through some Twisted magic I can't explain).

    The workaround I use is simple, just add #@UndefinedVariable to suppress the error:

    reactor.run() #@UndefinedVariable
    

    Voila. No more IDE errors.