2012-11-30 45 views
0

我嘗試從python wiki運行第一個例子,當我嘗試運行它:導入錯誤:無法導入名稱BaseHTTPRequestHandler

$ python BaseHttpServer.py 

我得到一個錯誤AttributeError的:「模塊」對象有沒有屬性「BaseHTTPRequestHandler」

我測試的Python 2.7.3 Linux上的Mageia 2 64位:

Traceback (most recent call last): 
    File "BaseHTTPServer.py", line 9, in <module> 
    import BaseHTTPServer 
    File "/home/vanveber/BaseHttpServer/BaseHTTPServer.py", line 14, in <module> 
    class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): 
AttributeError: 'module' object has no attribute 'BaseHTTPRequestHandler' 

而且它的Python 2.7.3在Windows 7 64位

Traceback (most recent call last): 
    File "BaseHTTPServer.py", line 11, in <module> 
    from BaseHTTPServer import BaseHTTPRequestHandler 
    File "C:\BaseHttpServer\BaseHTTPServer.py", line 11, in <module> 
    from BaseHTTPServer import BaseHTTPRequestHandler 
ImportError: cannot import name BaseHTTPRequestHandler 

BUT!

  1. BaseHttpServer是從標準Python庫一個類。
  2. 如果我從編寫並運行此代碼,則在Windows上的Python GUI工作正常!

什麼是問題,爲什麼?

回答

3

解決方案:重命名python文件。

說明: BaseHTTPServer是標準庫中的模塊。當你在你的本地目錄中稱爲BaseHTTPServer.py Python文件,你會隱藏標準庫模塊,並且可以不再進口,因爲該聲明

import BaseHTTPServer 

將不導入標準庫模塊,但本地的BaseHTTPServer.py模塊。

+0

**你是絕對正確的!**非常感謝! – dzav

相關問題