我尝试从python wiki运行第一个示例,当我尝试运行它时:
$ python BaseHttpServer.py
我收到错误 AttributeError:'module'对象没有属性'BaseHTTPRequestHandler'。
我在Linux Mageia 2 64位上的Python 2.7.3上测试过它:
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'
它在Windows 7 64位上的Python 2.7.3上
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!
什么是问题?为什么?!
答案 0 :(得分:3)
解决方案:重命名python文件。
说明: BaseHTTPServer是标准库中的模块。当您在本地目录中有一个名为BaseHTTPServer.py的python文件时,您将隐藏标准库模块,并且您无法再导入它,因为该语句
import BaseHTTPServer
不会导入标准库模块,而是导入本地BaseHTTPServer.py模块。