Python noob: "ImportError: no module named internet"

I am trying to get Python to curl working in my Ubuntu 11.04 box.

I did sudo apt-get install python-twisted

However, when I try to execute the following 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 get this error, which I cannot get at the bottom:

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

Any help is most appreciated.

+4
source share
2 answers

The problem is the name of your file. Python first looks for modules in your current directory. When you try to import twisted.internet it finds a file in your folder called twisted.py . But internet submodule cannot be found. If you rename your file, Python will load the correct twisted and everything will be found.

+20
source

You can change Python behavior for absolute imports instead of relative imports. Add a py file to the top of the file.

from __future__ import absolute_import

0
source

Source: https://habr.com/ru/post/1385506/


All Articles