'ImportError: "flask_sqlalchemy" could not be resolved

i'm unable to import flask_sqlalchemy even after installing flask-sqlachemy

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy 
 # ^this line shows error ImportError: "flask_sqlalchemy" could not be resolved

app = Flask(__name__)
@app.route('/')
def index():
    return render_template('index.html')

i'm not using any virtual environment and flask-sqlalchemy shows already installed

(base) sachin@sachin:~/Coding/flask/blog$ pip install Flask-SQLAlchemy
Requirement already satisfied: Flask-SQLAlchemy in /home/sachin/anaconda3/lib/python3.8/site-packages (2.4.4)
Requirement already satisfied: SQLAlchemy>=0.8.0 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Flask-SQLAlchemy) (1.3.20)
Requirement already satisfied: Flask>=0.10 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Flask-SQLAlchemy) (1.1.2)
Requirement already satisfied: Jinja2>=2.10.1 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (2.11.2)
Requirement already satisfied: itsdangerous>=0.24 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (1.1.0)
Requirement already satisfied: click>=5.1 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (7.1.2)
Requirement already satisfied: Werkzeug>=0.15 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (1.0.1)
Requirement already satisfied: MarkupSafe>=0.23 in /home/sachin/anaconda3/lib/python3.8/site-packages (from Jinja2>=2.10.1->Flask>=0.10->Flask-SQLAlchemy) (1.1.1)

pip freeze shows me its already installed -

Flask==1.1.2
Flask-SQLAlchemy==2.4.4


Solution 1:[1]

I had the same error. Changed my system python by doing the following in VS Code: Under the view menu select 'show command pallet'. One of the commands you can then select from the command palette is 'Python: Select Interpreter'. Selecting this option will allow you to choose which version of python to use.

Solution 2:[2]

This post is pretty old but I found a different solution that might help others out. For me, the issue was my IDE/Python (venv). I am using Visual Studio ver. 1.57.1 as of now on Windows 10.

  1. Open Visual Code.
  2. Go to 'View' on the toolbar.
  3. Click on 'Command Palette...' (or press CTRL + SHIFT + P on Windows).
  4. Search for: 'Python Select Interpreter'.
  5. Click on: 'Enter interpreter path...' followed by 'Find...'.
  6. Go to your (venv) scripts folder: project_folder/venv/Scripts.
  7. Select python.exe or pythonw.exe inside of your project_folder/venv/Scripts folder.

That's all.

The Flask-SQLAlchemy was installed in my virtual environment (venv). But my VSCode was using the system/PC Python interpreter to find the packages instead of the venv.

Hope this helps someone else out.

Solution 3:[3]

This is for people who do not use Anaconda (I had this issue and I solved it this way)

Your venv might be incorrect. Did you copy paste your venv from another directory?

venv saves path information, so when you run env\Scripts\activate, the venv believes it is in a different directory.

It will install the packages in the OLD directory. Make sure you generate a new py -m venv env for every app you create rather than copy pasting.

The solution is to delete your current venv directory, and run the py -m venv env command again. Then the packages will be installed in the current venv.

Solution 4:[4]

I had the same issue, this worked for me:

In cmd, cd to your project folder, (for me i had to activate venv, so it showed (env) C:\ etc\etc>)

Then install it here with:

pip3 install flask-SQLAlchemy

for some reason this took this noob 12 tries, but the give away was there was no sqla files in my site folder, and it kept showing the install had taken place in my python39 folder.

Solution 5:[5]

I had the same problem, but it worked when I changed to Python Interpreter under the command palette of the view tab. If you believe you have installed all required packages and it does not work then definitely try this.

Solution 6:[6]

One thing I have noticed is that if I change the python interpreter from command palette(ctrl+shift+p) to my virtual environment one it does not work. First I tried the command,

py -3 -m pip install flask-SQLALchemy

Instead of the above command, I upgrade pip by using,

python -m pip install --upgrade pip

then I tried using the following to import SQLAlchemy.

python -m pip install flask-SQLALchemy

and it worked for me.

Solution 7:[7]

When you have done changes, I mean change your python interpreter, delete your env or re-create install packages again. Please close your app.py file and re-open it. Some times it still gives your error.

Solution 8:[8]

For anyone looking for a more recent answer. I couldn't find a scripts folder. On my ubuntu machine I did this (in vscode):

  1. ctrl + shft + p
  2. search select python interpreter
  3. enter interpreter path
  4. navigate to your-app/venv/python3
  5. select python3

This resolved the import issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Not_Jimmy_Neutron
Solution 2
Solution 3 ThePrince
Solution 4 Paul Lemarchand
Solution 5 Bibek Gaire
Solution 6 MtkCodes
Solution 7 Anurag A S
Solution 8 Connor