Python Import Error - No module
I'm sorry this is basic. I've followed a fairly simple tutorial and get this error when trying to run flask locally:
Traceback (most recent call last):
File "/Users/james/opt/anaconda3/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/Users/james/Documents/GitHub/flask-app-blueprint/project/__init__.py", line 3, in <module>
from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'
However in the same environment when I ran pip list it tells me I have that module: Flask-SQLAlchemy 2.2
(venv) (base) james flask-app-blueprint % pip3 install flask-sqlalchemy
Requirement already satisfied: flask-sqlalchemy in ./venv/lib/python3.8/site-packages (2.5.1)
Requirement already satisfied: Flask>=0.10 in ./venv/lib/python3.8/site-packages (from flask-sqlalchemy) (1.0.2)
Requirement already satisfied: SQLAlchemy>=0.8.0 in ./venv/lib/python3.8/site-packages (from flask-sqlalchemy) (1.3.3)
Requirement already satisfied: Jinja2>=2.10 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (2.11.3)
Requirement already satisfied: itsdangerous>=0.24 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (0.24)
Requirement already satisfied: Werkzeug>=0.14 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (2.0.1)
Requirement already satisfied: click>=5.1 in ./venv/lib/python3.8/site-packages (from Flask>=0.10->flask-sqlalchemy) (7.0)
Requirement already satisfied: MarkupSafe>=0.23 in ./venv/lib/python3.8/site-packages (from Jinja2>=2.10->Flask>=0.10->flask-sqlalchemy) (2.0.1)
On top of that the code I have in my file looks correct based on what I see:
# IMPORTS
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
Where might I have gone wrong?
Solution 1:
The problem seems to be that when you run your flask script, the environment that it uses (anaconda) doesn't have the flask_sqlalchemy
package installed, which makes the script fail, but when you check whether it is or it isn't installed, you do so in the virtual environment venv
where it is indeed installed.