Flask:ModuleNotFoundError:没有名为“SocketServer”的模块

2024-04-19 21:48:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个奇怪的错误,我以前从未见过

我使用flask编写了一个小型http web服务器,当我运行时,出现了下面的错误

我使用的是虚拟环境,我的python版本是3.8

可能是什么

from flask import Flask, request

app = Flask(__name__)


@app.route("/")
def index():
    print(request.get_json())


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

python tools/fake.pyTraceback (most recent call last):
  File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/werkzeug/serving.py", line 58, in <module>
    from http.server import BaseHTTPRequestHandler
ModuleNotFoundError: No module named 'http.server'; 'http' is not a package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tools/fake.py", line 1, in <module>
    from flask import Flask, request
  File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/flask/__init__.py", line 16, in <module>
    from werkzeug.exceptions import abort
  File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/werkzeug/__init__.py", line 15, in <module>
    from .serving import run_simple
  File "/home/ubuntu/workspace/otc/venv/lib/python3.8/site-packages/werkzeug/serving.py", line 61, in <module>
    import SocketServer as socketserver
ModuleNotFoundError: No module named 'SocketServer'```

Tags: infrompyimporthttpflaskhomevenv
2条回答

这也许对你有帮助 `

pip uninstall Flask

pip uninstall Werkzeug

pip install Flask

pip install Werkzeug

`

问题来自第一次回溯:

ModuleNotFoundError: No module named 'http.server'; 'http' is not a package

您需要将http.py文件重命名为其他文件,因为它正在重写标准库http模块。要修复它,您需要执行以下操作:

  • 将文件http.py重命名为其他文件
  • 删除项目中的.pyc文件
find . -name "*.pyc" -delete
  • 再次运行程序

Python3使用socketserver,全部小写。Python2使用SocketServer

相关问题 更多 >