Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 852
Ryan Avatar asked Jul 31 '18 18:07

Ryan


People also ask

How do I know if SQLAlchemy is installed flask?

You can check if you have the Flask-SQLAlchemy package installed by running the pip show Flask-SQLAlchemy command.

How do I import SQLAlchemy?

Step 1 − Install Flask-SQLAlchemy extension. Step 2 − You need to import SQLAlchemy class from this module. Step 3 − Now create a Flask application object and set URI for the database to be used. Step 4 − Then create an object of SQLAlchemy class with application object as the parameter.

How install SQLAlchemy in Windows?

Installing SQLAlchemy on Windows You can install it by double clicking the . msi file. After you have installed Python on your Windows system, you can download the source code of SQLAlchemy from SQLAlchemy Download Page and install it using its setup.py script. Plain-Python build succeeded.


2 Answers

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
like image 148
Jadhav Gaurav Avatar answered Oct 05 '22 10:10

Jadhav Gaurav


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

like image 37
Wavesailor Avatar answered Oct 05 '22 10:10

Wavesailor