2011-12-09 40 views
4

我試圖讓Python扭曲到我的Ubuntu 11.04盒子上。Python noob:「ImportError:No module named internet」

我做sudo apt-get install python-twisted

然而,當我嘗試下面的代碼:

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() 

我得到這個錯誤,我不能去的底部:

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 

任何幫助最受讚賞。

+0

確實[這個答案可以幫助您?] [1]你有一個模塊名爲扭曲(在你的應用程序)? [1]:http://superuser.com/questions/161960/installed-python-module-is-not-found –

+0

這似乎我的系統上運行。嘗試導入扭曲,如果它的工作? –

回答

20

問題是您的文件的名稱。 Python首先在當前目錄中查找模塊。當您嘗試導入twisted.internet時,它會在您的文件夾中找到該文件,該文件名爲twisted.py。但是找不到internet子模塊。如果你重命名你的文件,Python將加載正確的twisted,一切都將完成。

+0

嗨,該文件現在被稱爲「eamorr.py」 - 我仍然得到相同的錯誤... – Eamorr

+10

你可能還有一個'twisted.pyc'文件。也刪除這個。 –

+0

好吧,我得到它的工作。最初我有一個名爲twisted.py的文件。我重命名了這個文件,但是我也必須刪除'twisted.pyc' ...非常感謝您的幫助, – Eamorr

0

可以將Python行爲更改爲絕對導入而不是相對導入。將以下內容添加到py文件的頂部。

from __future__ import absolute_import