[英]AttributeError: module 'sqlalchemy.orm' has no attribute 'DeclarativeMeta'
Getting the below error while running a flask application:运行 flask 应用程序时出现以下错误:
ubuntu@ip-10-50-50-190:~/RHS_US/application$ python3 run.py
Traceback (most recent call last):
File "run.py", line 1, in <module>
from portal import create_app
File "/home/ubuntu/RHS_US/application/portal/__init__.py", line 7, in <module>
from flask_sqlalchemy import SQLAlchemy
File "/home/ubuntu/RHS_US/application/rhs_us_venv/lib/python3.7/site-
packages/flask_sqlalchemy/__init__.py", line 5, in <module>
from .extension import SQLAlchemy
File "/home/ubuntu/RHS_US/application/rhs_us_venv/lib/python3.7/site-
packages/flask_sqlalchemy/extension.py", line 17, in <module>
from .model import _QueryProperty
File "/home/ubuntu/RHS_US/application/rhs_us_venv/lib/python3.7/site-
packages/flask_sqlalchemy/model.py", line 210, in <module>
class DefaultMeta(BindMetaMixin, NameMetaMixin, sa.orm.DeclarativeMeta):
AttributeError: module 'sqlalchemy.orm' has no attribute 'DeclarativeMeta'
I have also checked model.py under site packages and it does contain我还检查了站点包下的 model.py,它确实包含
class DefaultMeta(BindMetaMixin, NameMetaMixin, sa.orm.DeclarativeMeta):
class DefaultMeta(BindMetaMixin,NameMetaMixin,sa.orm.DeclarativeMeta):
Do pip freeze
to see packages versions.执行
pip freeze
查看软件包版本。 Maybe you have incompatible sqlalchemy and flask-sqlalchemy packages.也许你有不兼容的 sqlalchemy 和 flask-sqlalchemy 包。 I recently ocasionaly updated my sqlalchemy package from 1.23.X to 1.4.X and one of projects, dependent on 1.23.X failed to work correctly until I noticed it and degraded sqlalchemy. But because of this I got the same exception.
我最近偶尔将我的 sqlalchemy package 从 1.23.X 更新到 1.4.X,其中一个依赖 1.23.X 的项目无法正常工作,直到我注意到它并降级了 sqlalchemy。但正因为如此,我遇到了同样的异常。 I tried to install older versions of flask-sqlalchemy:
我尝试安装旧版本的 flask-sqlalchemy:
pip install Flask-SQLAlchemy==2.4.4
But it thrown the next exception:但它引发了下一个异常:
'_FakeStack' object has no attribute ' ident_func '
'_FakeStack' object 没有属性 ' ident_func '
So I tried to install 2.5.0:所以我尝试安装 2.5.0:
pip install Flask-SQLAlchemy==2.5.0
After this my flask project started without issues.在此之后,我的 flask 项目顺利启动。 Then I returned 2.5.1 and it also started without issues.
然后我返回 2.5.1,它也没有问题地启动。
So in my case the cause of the issue was degrading sqlalchemy package, which is gone after reinstalling Flask-SQLAlchemy package所以在我的例子中,问题的原因是降级 sqlalchemy package,重新安装 Flask-SQLAlchemy package 后就消失了
But I'm currently on python 3.11 windows. In your case the cause of the issue could differ.但我目前正在使用 python 3.11 windows。在您的情况下,问题的原因可能有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.