Home > Software design >  I can't import from flask_sqlalchemy and from flask_wtf.csrf even though I did pip install for
I can't import from flask_sqlalchemy and from flask_wtf.csrf even though I did pip install for

Time:11-08

The error when I run run.py is from flask_sqlalchemy import SQLAlchemy ModuleNotFoundError: No module named 'flask_sqlalchemy'

I am using windows 11 and visual studio code. All the other imports are working.

When I type pip freeze --local I get

bcrypt==3.2.0
cffi==1.15.0
click==8.0.3
colorama==0.4.4
csrf==0.1b1
Flask==2.0.2
Flask-Login==0.5.0
Flask-SQLAlchemy==2.5.1
Flask-WTF==0.15.1
greenlet==1.1.2
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
pycparser==2.20
six==1.16.0
SQLAlchemy==1.4.26
Werkzeug==2.0.2
WTForms==2.3.3

I have tried pip uninstall and pip install for both flask_sqlalchemy and flask_wtf.csrf.

if you want I can upload the entire code to github.

To setup the virtual environment I follow the steps from this link . https://code.visualstudio.com/docs/python/tutorial-flask To install the virtual environment

I even checked the environment variables Here is the path C:\Users\nmyle\AppData\Local\Programs\Python\Python310

Here are the environment variables. C:\Users\nmyle\AppData\Local\Programs\Python\Python310\Scripts
C:\Users\nmyle\AppData\Local\Programs\Python\Python310\

Oddly enough when I click on the non recommended virtual environment Everything works but flask-login. list of virtual environments

CodePudding user response:

You need to activate the virtual environment in your console before running pip

source ./venv/Scripts/activate

CodePudding user response:

You need to check which pip you are using through the pip --version command.

It looks like, you have selected the virtual environment, but you have not installed the pip module on it, so it will use the global pip. Then modules will be installed on the global one, but the python interpreter was the virtual environment, so it will output ModuleNotFoundError.

  • Related