BUG 速报:Windows 执行 flask run 报错 ModuleNotFoundError

855 阅读1分钟

如果你更新了 Werkzeug 到最新的 0.15.5 版本,在 Windows 下执行 flask run 会出现类似下面的报错:

d:\repos\flask-examples\.venv\scripts\python.exe: Error while finding module specification for 
'D:\\repos\\flask-examples\\.venv\\Scripts\\flask' (ModuleNotFoundError: No module named 'D:\\repos\\flask-examples\\')

或是:

C:\Users\greyli\.virtualenvs\flask-examples-qatl1Peb\Scripts\python.exe: Error while finding module specification for 
'C:\Users\greyli\\.virtualenvs\\flask-examples-qatl1Peb\\Scripts\\flask' (ModuleNotFoundError: No module named 'C:\Users\greyli\\')

触发这个错误同时要满足的条件一般还有:

  • 开启了调试模式,即 FLASK_ENV=development
  • 使用 Pipenv 或使用 virtualenv 时虚拟环境目录包含点

解决方法:

  • 降级 Werkzeug 到 0.15.4 版本(这个 bug 将在 0.15.6 版本修复)。
  • 如果不想降级,那就临时使用 python -m flask run 命令启动程序

具体信息见 https://github.com/pallets/werkzeug/issues/1619