Ryan
Ryan

Reputation: 1432

ImportError: No module named 'flask_sqlalchemy' w/ 2 Versions of Python Installed

Tried running a file with the following imports:

from flask_sqlalchemy import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

Received the following error:

ImportError: No module named 'flask_sqlalchemy'

SQLAlchemy is installed. Still, I tried to reinstall into the directory in which it will be used. I got this:

The directory '/Users/_/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/_/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: Flask-SQLAlchemy in /Library/Python/2.7/site-packages (2.3.2)
Requirement already satisfied: Flask>=0.10 in /Library/Python/2.7/site-packages (from Flask-SQLAlchemy) (1.0.2)
Requirement already satisfied: SQLAlchemy>=0.8.0 in /Library/Python/2.7/site-packages (from Flask-SQLAlchemy) (1.2.10)
Requirement already satisfied: Jinja2>=2.10 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (2.10)
Requirement already satisfied: itsdangerous>=0.24 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (0.24)
Requirement already satisfied: Werkzeug>=0.14 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (0.14.1)
Requirement already satisfied: click>=5.1 in /Library/Python/2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy) (6.7)
Requirement already satisfied: MarkupSafe>=0.23 in /Library/Python/2.7/site-packages (from Jinja2>=2.10->Flask>=0.10->Flask-SQLAlchemy) (1.0)

The bit about me not owning the directory is incorrect. I'm the only one on this machine. I own everything.

Anyway, I go back to rerun the file and get the same error message. So, it's installed, but not installed or, at the very least, not available to me.

One error message I saw when I commented out one of the import statements read as follows:

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py

I have no clue how to fix this and get SQLAlchemy up and running. I've burned over 1.5 hours on it. The last error listed suggests having 2 versions of python may have something to do with it.

Your thoughts on a remedy would be appreciated.

Upvotes: 14

Views: 61902

Answers (16)

dooropening
dooropening

Reputation: 1

environment: win10, pycharm, install python3.11, use venv.

problem:venv interpreter had intall flask_sqlalchemy,but when the page runs,it hints could not find module flask_sqlalchemy.

solution:change interpreter to the globle version(in pycharm), and install flask_sqlalchemy, then change interpreter to the venv version.

Upvotes: 0

Roberto Rios
Roberto Rios

Reputation: 41

this worked for me, hope you find it usefull:

first, uninstall flask and flask_sqlalchemy globally, open a cmd and run:

pip uninstall flask
pip uninstall flask_sqlalchemy

then, activate your project's virtual environment, in my case:

.\venv\Scripts\activate

then install flask and flask_sqlalchemy locally. Make sure at the beginning of the command line says something like (venv):

pip install flask
pip install flask_sqlalchemy

Upvotes: 1

NATOR Thumbu
NATOR Thumbu

Reputation: 1

if you are on windows just change your interpreter from Python/python3 and use the venv Python or venv Python3 by clicking on problems and and right clicking the problem and selecting a different interpreter

Upvotes: 0

Chandu Arepalli
Chandu Arepalli

Reputation: 101

Reason

This happens when run the flask from flask run command in the virtual environment

  • Error occurs due to pip install flask or installed flask in globally
  • When we run flask run, the python or python3 complier first check global package of flask and if not exist flask then check in the virtual environment
  • If python or python3 detect the flask in globally then flask package will import other packages like SQLAlachemy, Migrate, etc from global packages

Solution

  • Open new terminal or deactivate virtual environment

  • uninstall the flask in the global packages pip uninstall flask or pip3 uninstall flask

  • Then go back to virtual environment terminal

    • Run the flask flask run

    or

  • activate virtual environment and run the commands

    In Ubuntu/ Mac

    export FLASK_APP=app
    export FLASK_ENV=development
    flask run
    

    In Windows

    SET FLASK_APP=app
    SET FLASK_ENV=development
    flask run
    

Upvotes: 3

Chandu Arepalli
Chandu Arepalli

Reputation: 101

This issue main occurs when we are run the flask with cmd flask run in the virtual environment

Reason

This happens when run the flask from flask run command

flask-sqlalchemy consider the path of python or python3 rather than virtaul environment installed packages path

Solution

  • Open new terminal or deactivate virtual environment
  • install flask-sqlalchemy in global environment pip install flask-sqlalchemy
  • Then activate virtual environment and run the commands
    • In Ubuntu/ Mac

      export FLASK_APP=app
      export FLASK_ENV=development
      flask run
      
    • In Windows

      SET FLASK_APP=app
      SET FLASK_ENV=development
      flask run
      

Upvotes: 4

Faizan Usmani
Faizan Usmani

Reputation: 11

I was using PyCharm and encountered the same problem. The solution to this is to open the terminal and make your current directory the file which contains your python program. Then after that install the required package i.e. pip install flask_sqlalchemy . After that it works.

Upvotes: 0

Ahmed Damasy
Ahmed Damasy

Reputation: 701

for anaconda users.

I tried almost all the solutions above, no one helped me,

but I tried to open anaconda navigator and launched vsCode from anaconda navigator, that's what solved the problem, I think there are some conflicts between working environments.

Note: you should keep the anaconda navigator opened !!!!

Upvotes: 0

Alemoh Rapheal Baja
Alemoh Rapheal Baja

Reputation: 762

If you've installed and nothing seems to change add this line of code to init.py

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

to turn off the track notifications error

Upvotes: 0

RjCola28
RjCola28

Reputation: 1

I ran into this issue recently. The following steps resolved my issue:

Ensure you are importing flask

from flask import Flask from flask_sqlalchemy import SQLAlchemy

Flask sqlalchemy tutorial https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/

Also, ensure your env is "activated" before executing your code.

I am using Python 2.7 and pip 20.0.2

Upvotes: 0

Imran Said
Imran Said

Reputation: 365

It's quite likely that you might have installed the particular project in a virtual environment but then forgot to assign the venv intepreter as your project's intepreter. If you're using pycharm, go to File > Settings > Project Intepreter, and select the correct intepreter for your project from the dropdown list.

The window would also show you all the packages installed on that particular intepreter so you can confirm that you have actually installed SQLAlchemy.

Upvotes: 1

Ryan
Ryan

Reputation: 1432

Ultimately, I resolved this issue ages after I posted the above question.

The fix was to run all package updates and installs thru Anaconda and do my work in Spyder.

The lesson learned was simple: Once you start using Anaconda as your go-to environment for all things Python, all updates -- made via conda install or pip -- will be orchestrated and placed in your system by Anaconda by default.

Upvotes: 2

Hossein Kalbasi
Hossein Kalbasi

Reputation: 1861

Possibly it is a pip path issue. If using conda virtual env (with anaconda or miniconda), check which pip and which python in linux (find your pip and python path) and make sure they're aligned. Fix your pip path issue or instead of pip install flask_sqlalchemy do a

<ANACONDA or MINICONDA PATH>/envs/<ENV_NAME>/bin/pip install flask_sqlalchemy

in order to install the package in the correct place.

Upvotes: 2

Jadhav Gaurav
Jadhav Gaurav

Reputation: 578

I think you have a mismatch between your pip and python versions. check your pip version pip --version, if it is pip3
you can try this,

sudo apt-get install python3-sqlalchemy

This should work.

~$ python3

>>import sqlalchemy

Upvotes: 9

Serg
Serg

Reputation: 188

If you are on mac, try pip install flask-sqlalchemy instead of pip3 install flask-sqlalchemy.

It works with some warnings, but was able to succesfully connect to the database and create the tables. "FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning."

Upvotes: 7

liangli
liangli

Reputation: 1197

This works for me.

  1. If you use virtual environment, firstly you should check whether you are under right environment when you are installing new packages. Since if you use pycharm as IDE and conda as virtual env manager, pycharm won't automatically active the environment of your interpreter in terminal. Example: (base) -> (myenv)
  2. Use pip3 as package installer. Sometimes problems popped up because of different versions of python in your computer. pip3 install flask_sqlalchemy

packages installed by pip will be under python2.x/site-packages, while that of pip3 will be under python3.x/site-packages

Upvotes: 0

Wavesailor
Wavesailor

Reputation: 676

Did you install flaskext.sqlalchemy? It looks like you may have only installed the SQLAlchemy package and NOT the Flask Extension. Try pip install Flask-SQLAlchemy

Upvotes: 4

Related Questions