简体   繁体   中英

AttributeError: module 'sqlalchemy.orm' has no attribute 'DeclarativeMeta'

Getting the below error while running a flask application:

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

class DefaultMeta(BindMetaMixin, NameMetaMixin, sa.orm.DeclarativeMeta):

Do pip freeze to see packages versions. Maybe you have incompatible sqlalchemy and flask-sqlalchemy packages. 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. I tried to install older versions of flask-sqlalchemy:

pip install Flask-SQLAlchemy==2.4.4

But it thrown the next exception:

'_FakeStack' object has no attribute ' ident_func '

So I tried to install 2.5.0:

pip install Flask-SQLAlchemy==2.5.0

After this my flask project started without issues. Then I returned 2.5.1 and it also started without issues.

So in my case the cause of the issue was degrading sqlalchemy package, which is gone after reinstalling Flask-SQLAlchemy package

But I'm currently on python 3.11 windows. In your case the cause of the issue could differ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM