Reputation: 71
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
Upvotes: 5
Views: 24439
Reputation:
Had the same issue, resolved using:
pip3
to install flask
and flask_sqlalchemy
by running:pip3 install flask_sqlalchemy flask
CMD + Shift + P --> Python: Select Interpreter --> selecting appropriate python version for the venv (preferably python10)
Upvotes: 1
Reputation: 113
For anyone looking for a more recent answer. I couldn't find a scripts folder. On my ubuntu machine I did this (in vscode):
This resolved the import issue.
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 890
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
.
Upvotes: 2
Reputation: 279
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.
project_folder/venv/Scripts
.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.
Upvotes: 5
Reputation: 41
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.
Upvotes: 3
Reputation: 121
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.
Upvotes: 12