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

Ika*_*ari 4 python module http python-3.x

当我使用时:

>>> import http.server
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

有一个错误如下:

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
Run Code Online (Sandbox Code Playgroud)

请帮忙!

Bha*_*Rao 12

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

要解决

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