4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python初心者がNo module named 'http.server'で詰まった話

Last updated at Posted at 2015-07-09

環境

  • 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によっては、
他のファイル名でもアウトになることがあるので注意しましょう。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?