Forums

Apparently flask_sqlalchemy is not found, but it is installed

Hi, I already checked similar posts in this forum, but the problem still persists. I'm trying to host my very first flask app here, but after uploading I'm getting the following error:

Error running WSGI application
ModuleNotFoundError: No module named 'flask_sqlalchemy'
File "/var/www/[name]wsgi.py", line 16, in <module>
from app import app as application  # noqa

File "path/to/mysite/app.py", line 2, in <module>
from flask_sqlalchemy import SQLAlchemy

However, when I install my requirements.txt with:

pip3.10 install -r requirements.txt --user

I get the following:

Requirement already satisfied: flask_sqlalchemy in /usr/local/lib/python3.10/site-packages (from -r requirements.txt (line 4)) (2.5.1)

I'm very confused why flask_sqlalchemy is not found. Can you please help me?

Any help is much appreciated. Thanks

Could you show us the exact error you see at the bottom of your error log?

Thanks for the fast reply. I didn't check that so far. Here it is:

Error running WSGI application
AttributeError: module 'sqlalchemy' has no attribute '__all__'
File "/var/www/christianteppich_pythonanywhere_com_wsgi.py", line 16, in <module>
from app import app as application  # noqa
File "/home/ChristianTeppich/mysite/app.py", line 9, in <module>
db = SQLAlchemy(app)
File "/usr/local/lib/python3.10/site-packages/flask_sqlalchemy/__init__.py", line 758, in __init__
_include_sqlalchemy(self, query_class)

File "/usr/local/lib/python3.10/site-packages/flask_sqlalchemy/__init__.py", line 112, in _include_sqlalchemy
for key in module.__all__:

So that rather looks like a version version mismatch -- check versions of flask_sqlalchemy and SQLAlchemy and if they are compatible.

Yes, I had some troubles generating the requirements.txt since flask_sqlalchemy wasn't found locally:

WARNING: Import named "flask_sqlalchemy" not found locally. Trying to resolve it at the PyPI server.
WARNING: Import named "flask_sqlalchemy" was resolved to "Flask-SQLAlchemy:3.1.1" package 
(https://pypi.org/project/Flask-SQLAlchemy/).
Please, verify manually the final list of requirements.txt to avoid possible dependency confusions.

When installing it on Pythonanywhere I took away the versions for flask_sqlalchemy in the requirements.txt and let pip find the version itself. Apparently that yields the issue.

How can I check which versions are compatible to Flask 2.1.2? In this link to flask-sqlalchemy it shows that I need Flask 0.12+ which I have with SQLAlchemy 1.0.10+ which I also have:

Flask is 2.1.2

flask_sqlalchemy is 2.5.1

SQLAlchemy is 2.0.23

So I don't see why they are not compatible. Can you kindly support me in how I can check the compatibility further?

Check out this Stack Overflow post -- it looks like you need Flask-SQLAlchemy>=3.0.2 to work with SQLAlchemy 2.0 or higher.

Ok. Thank you very much for this. I will try to install Flask-SQLAlchemy 3.0.2.

I install the newer versions and now it is working. Thank you so much for your help. Case solved!

Excellent, glad we could help!