当我从空闲导入`http.server`时它工作,但是当我运行一个有'import http.server`的python文件时出现错误

时间:2015-02-21 13:22:00

标签: python http python-3.x module

当我使用时:

>>> import http.server
IDLE中的

没有任何错误。
但是当我使用这段代码时:

import http.server
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer

def run(server_class = HTTPServer, handler_class = BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd=server_class(server_address, handler_class)
    httpd.serve_forever()


run()

存在以下错误:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2195, 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 "/home/toton/Projects/http.py", line 1, in <module>
    import http.server
  File "/home/toton/Projects/http.py", line 1, in <module>
    import http.server
ImportError: No module named 'http.server'; 'http' is not a package

请帮助!

1 个答案:

答案 0 :(得分:10)

您已将文件命名为http.py,因此它会覆盖原始模块http

解决

  • 将文件名更改为其他内容
  • 删除pyc文件
  • 再次运行程序