代码之家  ›  专栏  ›  技术社区  ›  Ikari

当我从空闲状态导入“http.server”时,它会正常工作,但当我运行一个具有“importhttp.server”的python文件时,会出现一个错误

  •  6
  • Ikari  · 技术社区  · 10 年前

    当我使用:

    >>> 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 回复  |  直到 10 年前
        1
  •  16
  •   Bhargav Rao rlgjr    10 年前

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

    要解决

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