一、
ModuleNotFoundError: No module named ‘flask._compat’
是因为flask版本过高了
降低版本
pip install flask_script==2.0.6
二、
ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. flask-sqlalchemy 3.0.5 requires flask>=2.2.5, but you have flask 1.1.2 which is incompatible.
由于flask-sqlalchemy版本过高了
pip3 install flask-script==2.0.6
三、
ImportError: cannot import name ‘json’ from ‘itsdangerous’
ImportError: cannot import name ‘app_ctx’ from ‘flask.globals’
一般是flask版本过低导致:
pip install flask==2.0.2
四、
ModuleNotFoundError: No module named ‘flask._compat’
解决方法1:降低flask版本,1.1.2即可
pip3 install flask==1.1.2
解决方法2:
使用高版本的flask,在flask_script/init.py 里面的
from ._compat import text_type
改成
from flask_script._compat import text_type
五、
AttributeError: module ‘sqlalchemy.orm’ has no attribute ‘DeclarativeBase’
降级了 sqlalchemy
pip install Flask-SQLAlchemy==2.5.0
六、
ImportError: cannot import name ‘_endpoint_from_view_func’ from ‘flask.helpers’
第一种
pip install Flask-RESTful==0.3.9
第二种
您可以定义自己的_endpoint_from_view_func或使用flask.scaffold._endpoint_from_view_func
monkey.py
def _endpoint_from_view_func(view_func):
"""Internal helper that returns the default endpoint for a given
function. This always is the function name.
"""
assert view_func is not None, "expected view func if endpoint is not provided."
return view_func.__name__
# noinspection SpellCheckingInspection
def patch_restx(endpoint_from_view_func: bool = True) -> None:
"""
Nasty hacks are here
:param endpoint_from_view_func: Add missing function into flask for backward compatibility with `flask-restx`
"""
if endpoint_from_view_func:
func_name = "_endpoint_from_view_func"
try:
import pkg_resources
packages = pkg_resources.working_set.by_key
if "flask" in packages and "flask-restx" in packages:
flask_version = tuple(map(int, packages["flask"].version.split(".")))
flask_restx_version = tuple(map(int, packages["flask-restx"].version.split(".")))
if (2, 0, 0) <= flask_version and (1, 0, 0) > flask_restx_version:
import flask
if not hasattr(flask.helpers, func_name):
setattr(flask.helpers, func_name, _endpoint_from_view_func)
except ImportError:
print(f"skipping monkey patch of {func_name}")
七、ImportError: cannot import name ‘_request_ctx_stack’ from ‘flask’
修改 [你的虚拟环境]\Lib\site-packages\flask_script\commands.py
from flask import _request_ctx_stack 改为 from flask import request_ctx
修改 [你的虚拟环境]\Lib\site-packages\flask_init_.py
导入 from .globals import _no_app_msg, request_ctx
八、AttributeError: module ‘sqlalchemy’ has no attribute ‘all’. Did you mean: ‘file’?
可能与this issue有关,关于flask_sqlalchemy与sqlalchemy的兼容性,原因是SqlAlchemy2.0中删除了__all__属性。
您可以将sqlalchemy固定到比2.0更早的版本(1.4是稳定的),或者升级到比3.0.2更新的Flask-SqlAlchemy版本。
pip install sqlalchemy===1.4.0