Search code examples
pythontcptwistedosx-mountain-lion

Can't import twisted.internet.protocol module on mountain lion


I am trying to implement a TCP server and using python script. So I created a server.py on my desktop and imported the following modules:

from twisted.internet.protocol import Factory,Protocol

It's already failing at that point, because if I run: python server.py, I got the following output:

Traceback (most recent call last):
  File "server.py", line 4, in <module>
    from twisted.internet.protocol import Factory,Protocol
ImportError: No module named twisted.internet.protocol

I also use sys.path to print my Python directories and I got these outputs:

['/Users/Alfred/Desktop',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages']

What could have gone wrong with my python setup?

Is it because of the privilege of my account? I logged in as the admin on this machine.


Solution

  • Based on your sys.path, you've installed Python from python.org, or compiled your own, rather than using the built-in Python that has Twisted pre-installed. If you install your own Python, you have to install your own version of Twisted into that version of Python. You can use pip or easy_install or download directly from the Twisted site and run setup.py.

    type -p python in a shell will tell you which version you're using; I suspect it will be in /usr/local/bin/.

    /usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol' ought to succeed. If not, then I don't know what you've done to your sytem Python installation :).