環境
- windows 7 64bit
- python 3.4.3
コード
http.py
# -*- coding: utf-8 -*-
import http.server
server_address = ("", 8000)
handler_class = http.server.CGIHTTPRequestHandler #1 ハンドラを設定
server = http.server.HTTPServer(server_address, handler_class)
server.serve_forever()
上記のように、localhostの8000でLISTENするhttpサーバを立てた時に、
以下のメッセージが出力されて悩んだのでTips。
エラーメッセージ
error_log
C:\Python34\python.exe C:/Users/xxx/PycharmProjects/untitled/http.py
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/xxx/PycharmProjects/untitled/http.py", line 3, in <module>
import http.server
File "C:\Users\xxx\PycharmProjects\untitled\http.py", line 3, in <module>
import http.server
ImportError: No module named 'http.server'; 'http' is not a package
原因
ファイル名をhttp.pyとしてしまったので、test.pyなどに変更するとうまくいく。
※import http.serverのhttpと競合した模様
今回はhttpだがimportする外部のmoduleによっては、
他のファイル名でもアウトになることがあるので注意しましょう。