Home > database >  Error importing SQLAlchemy from flask_sqlalchemy: AttributeError: module 'sqlalchemy.orm'
Error importing SQLAlchemy from flask_sqlalchemy: AttributeError: module 'sqlalchemy.orm'

Time:01-14

I am trying to use SQL Alchemy as part of a flask project. I am following a tutorial and got it all working on my Macbook with VSCode but now want to port the project to my Raspberry Pi so I can have it running long term. I have installed all the same packages as I used on my Macbook but when issuing the import command to import SQLAlchemy from flask_sqlalchemy, I get the following error:

Python 3.7.16 (default, Jan 12 2023, 13:19:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_sqlalchemy import SQLAlchemy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/stocktracker101/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 5, in <module>
    from .extension import SQLAlchemy
  File "/home/pi/stocktracker101/lib/python3.7/site-packages/flask_sqlalchemy/extension.py", line 17, in <module>
    from .model import _QueryProperty
  File "/home/pi/stocktracker101/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 googled this error but no hits of any help.

Below is my pip list showing all packages installed in my venv:

Package             Version
------------------- -----------
appdirs             1.4.4
beautifulsoup4      4.11.1
bs4                 0.0.1
certifi             2022.12.7
charset-normalizer  2.1.1
click               8.1.3
cssselect           1.2.0
fake-useragent      1.1.1
feedparser          6.0.10
Flask               2.2.2
Flask-SQLAlchemy    3.0.2
gunicorn            20.1.0
idna                3.4
importlib-metadata  4.13.0
importlib-resources 5.10.2
itsdangerous        2.1.2
Jinja2              3.1.2
lxml                4.9.2
MarkupSafe          2.1.1
numpy               1.21.6
pandas              1.3.5
parse               1.19.0
pip                 22.3.1
psycopg2            2.8.4
psycopg2-binary     2.8.4
pyee                8.2.2
pyppeteer           1.0.2
pyquery             2.0.0
python-dateutil     2.8.2
pytz                2022.7
requests            2.28.1
requests-html       0.10.0
setuptools          47.1.0
sgmllib3k           1.0.0
six                 1.16.0
soupsieve           2.3.2.post1
SQLAlchemy          1.3.15
tqdm                4.64.1
typing_extensions   4.4.0
urllib3             1.26.14
w3lib               2.1.1
websockets          10.4
Werkzeug            2.2.2
yahoo-fin           0.8.9.1
zipp                3.11.0

Any help much appreciated! Rob.

CodePudding user response:

The current minimum version of sqlalchemy required is 1.4.18 https://github.com/pallets-eco/flask-sqlalchemy/blob/6e58435fb00445bfb3ec211f60a872716165249a/pyproject.toml#L19. You will need to try pip install sqlalchemy --upgrade; I'm not sure why the dependencies weren't resolved properly when you installed your libraries.

  • Related