解决ModuleNotFoundError: No module named ‘flask._compat的问题

 更新时间:2024年06月01日 10:56:27   作者:T  
这篇文章主要介绍了解决ModuleNotFoundError: No module named ‘flask._compat的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

ModuleNotFoundError: No module named ‘flask._compat

使用flask_script管理flask启动

注意:

  • 最后两个错有解决没有略过至于报错顺序
  • 在解决完flask版本 Jinja2 有可能会报错 看自己的版本 最后一个Jinja2错误是复现出来的

代码:

from flask import Flask
import config
from flask_script import Manager

app=Flask(__name__)

#app.config.from_object(config.MyConfig)

manager = Manager(app)



if __name__ == '__main__':
    manager.run()

报错信息

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/flask_script/__init__.py", line 15, in <module>
    from flask._compat import text_type
ModuleNotFoundError: No module named 'flask._compat'

查看安装的包

pip  freeze  

发现安装的flask的版本过高没有 直接拉取的话就是最新版的


版本降低即可解决

pip3 install flask==1.1.4 

版本更新完

接着又报错

Traceback (most recent call last):
  File "test3.py", line 1, in <module>
    from flask import Flask
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/jinja2/__init__.py", line 12, in <module>
    from .environment import Environment
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/jinja2/environment.py", line 25, in <module>
    from .defaults import BLOCK_END_STRING
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/jinja2/defaults.py", line 3, in <module>
    from .filters import FILTERS as DEFAULT_FILTERS  # noqa: F401
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/jinja2/filters.py", line 13, in <module>
    from markupsafe import soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' 

查看markupsafe版本发现是2.1.1

markupsafe版本降低

python -m pip install markupsafe==2.0.1

来继续报错

Traceback (most recent call last):
  File "test3.py", line 1, in <module>
    from flask import Flask
  File "/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/Users/tly/opt/anaconda3/envs/FlaskProject/lib/python3.8/site-packages/jinja2/__init__.py)

继续降版本

pip install Jinja2==3.0.0

到此为止输入命令python test3.py runserver项目启动起来

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

最新评论