Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named twisted.internet

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?

like image 232
user1700184 Avatar asked Jul 04 '13 19:07

user1700184


2 Answers

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
like image 171
Milev Avatar answered Sep 18 '22 16:09

Milev


I figured out why this error was happening. For some reason, using apt-get to install a python package was not installing it right.

So, I had to download a tar ball and install the package from them.

I downloaded Twisted tar from here.

I did a tar xjf Twisted-13.1.0.tar.bz2 - this created a directory called Twisted-13.1.0

next, cd Twisted-13.1.0 Finally, python setup.py install

This gave me an error. Twisted required another package called zope.interface. So, I downloaded tar ball for zope.interface from here. Then, ran this command tar xzf zope.interface-3.6.1.tar.gz That created a folder called zope.interface-3.6.1. So, cd into zope.interface-3.6.1 and run python setup.py install

Note: Depending on your user's rights, you may want to do these commands in sudo mode. Just add the keyword sudo before every command.

like image 28
user1700184 Avatar answered Sep 18 '22 16:09

user1700184