Search code examples
pythontwisted

ImportError: cannot import name process (Twisted)


I'm using Mac OS X 10.9.1, and I remember installing Python some time ago (I can't remember why, given that Mac OS X comes with it). For some reason Twisted wasn't installed, so I installed Zope and Twisted.

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

The problem is, when I run this code:

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

class IphoneChat(Protocol):
    def connectionMade(self):
        print "a client connected"

factory = Factory()
factory.protocol = IphoneChat
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor().run

I get this error:

Traceback (most recent call last):
  File "/Users/Mattieman/Desktop/server.py", line 4, in <module>
    from twisted.internet import reactor
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/reactor.py", line 38, in <module>
    from twisted.internet import default
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/default.py", line 56, in <module>
    install = _getInstallFunction(platform)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/default.py", line 52, in _getInstallFunction
    from twisted.internet.selectreactor import install
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/selectreactor.py", line 18, in <module>
    from twisted.internet import posixbase
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/posixbase.py", line 53, in <module>
    from twisted.internet import process, _signals
ImportError: cannot import name process

So I took a look in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet

...and it appears that process.py is missing.

What do I do?

There seem to be other files missing as well, like _baseprocess.py

I used setup3.py, if it helps...


Solution

  • You're using Python 2.6. You shouldn't use setup3.py. You also shouldn't ever use a distutils setup.py-type script to install Python libraries into OS paths (like /Library/Frameworks/Python.framework/Versions/2.6/).

    You may have a randomly corrupted Twisted installation on your system now (OS X itself ships with a copy of Twisted). Or you may just have some garbage in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted that you need to clean up. It's difficult to say which (which is why you should never use setup.py to install things - it leaves a big mess).

    After you clean up that mess (I can't give you many tips on how to do this apart from "reinstall your OS X", sorry), you have a few options:

    • Use the version of Twisted distributed with your OS. Don't try to install a different version.
    • Treat your home directory as a simpler "virtual environment" and run setup.py install --user (--user tells it to leave its mess inside ~/.local where at least it won't ruin your OS install).
    • Create a virtualenv and install a new version of Twisted there

    Of these, you probably want to go with virtualenv.

    Also note that you shouldn't run setup3.py unless you're trying to install Twisted for Python 3. And if you're trying to do that then I'll warn you that only part of Twisted is ported - and it's a very small part. For example, it's not process support (hence the import error you're getting).