python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > ModuleNotFoundError: No module named ‘flask._compat

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

作者:T

这篇文章主要介绍了解决ModuleNotFoundError: No module named ‘flask._compat的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

ModuleNotFoundError: No module named ‘flask._compat

使用flask_script管理flask启动

注意:

代码:

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项目启动起来

总结

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

您可能感兴趣的文章:
阅读全文