ImportError: No module named twisted.internet

user1700184 picture user1700184 · Jul 4, 2013 · Viewed 58.4k times · Source

I installed python 2.7.5 which is working fine.

I then install scrapy (which, I think, uses twisted internally). My scrapy spider is also working fine.

I installed twisted:

sudo apt-get install python-twisted

Then, I created a sample program using Echo Server code shown here

Here is the code

from twisted.internet import protocol, reactor


class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(1234, EchoFactory())
reactor.run()

I try to run this code using this command:

$ python twistedTester.py 
Traceback (most recent call last):
  File "twistedTester.py", line 1, in <module>
    from twisted.internet import protocol, reactor
ImportError: No module named twisted.internet

Can anyone help me with how I can debug why my twisted package is not being picked up by Python installation?

Answer

Milev picture Milev · Nov 7, 2014

If you use pip just try:

pip install twisted

The same works with w3lib and lxml.

On some *nix systems this might give you a permission error. If that happens, try:

sudo -H pip install twisted