模块未找到错误: 没有名为 'BaseHTTPServer' 的模块。

23

我正在尝试跟随一篇关于如何使用web.py的在线教程,我已经安装好了它,但不幸的是,使用这段代码会产生一个讨厌的错误。

我的代码...

import web

urls = (
    '/(.*)', 'index'
)

app = web.application(urls, globals())


class index:
    def GET(self, name):
        return "Hello", name, '. How are you today?'

if __name__== "__main__":
    app.run()

我的错误:

C:\Users\User\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/User/PycharmProjects/Webprojects/main.py

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/Webprojects/main.py", line 15, in <module>
    app.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web\application.py", line 312, in run
    return wsgi.runwsgi(self.wsgifunc(*middleware))
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web\wsgi.py", line 59, in runwsgi
    return httpserver.runsimple(func, server_addr)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web\httpserver.py", line 154, in runsimple
    func = LogMiddleware(func)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\web\httpserver.py", line 296, in __init__
    from BaseHTTPServer import BaseHTTPRequestHandler
ModuleNotFoundError: No module named 'BaseHTTPServer'

Process finished with exit code 1

看起来那个教程是基于Python 2版本的。Python 3没有名为“BaseHTTPServer”的模块。请参阅此链接:https://docs.python.org/3/library/http.server.html - Himal
我认为你不能使用任何工具来进行转换。你需要的是web.py的python3版本,请参见https://dev59.com/hKHia4cB1Zd3GeqPT3j5。 - Teemu Risikko
1个回答

25

这个导入语句在Python 3中无法正常工作,因为BaseHTTPServer已经被移动到http.server

在您的具体情况中,您应该将web.py更新到与Python 3兼容的当前版本。


4
我遇到了"ModuleNotFoundError: No module named 'http.server.HTTPServer'; 'http.server' is not a package"的错误。 - Tilek
19
使用http.server而不是BaseHTTPServer效果更好。 - Tilek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接