I try to run first example from python wiki and when I try to run it:
$ python BaseHttpServer.py
I get an error AttributeError: ‘module’ object has no attribute ‘BaseHTTPRequestHandler’.
I tested it on Python 2.7.3 on Linux Mageia 2 64-bit:
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'
And it on Python 2.7.3 on Windows 7 64-bit
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!
- BaseHttpServer is a class from standard Python library.
- If I write and run this code from Python GUI on Windows it works correctly!
What is a problem and why?!
Solution: Rename the python file.
Explanation: BaseHTTPServer is a module in the standard library. When you have a python file called BaseHTTPServer.py in your local directory, you will hide the standard library module, and you can no longer import it, because the statement
will not import the standard library module, but the local BaseHTTPServer.py module.