[Solved] ImportError: No module named sqlalchemy

I’m unable to find a module in python ,though easy_install says its already installed.
Any idea how to resolve this isseue?

$ python -c "from flaskext.sqlalchemy import SQLAlchemy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named sqlalchemy

$ python -V
Python 2.7

$ sudo easy_install sqlalchemy
Searching for sqlalchemy
Best match: SQLAlchemy 0.7.7
Adding SQLAlchemy 0.7.7 to easy-install.pth file

Using /usr/lib/python2.7/site-packages
Processing dependencies for sqlalchemy
Finished processing dependencies for sqlalchemy

$ sudo pip install SQLAlchemy –upgrade Requirement already
up-to-date: SQLAlchemy in /usr/lib/python2.7/site-packages Cleaning

Though pip says it’s installed.But I can’t find them in sys.path output.

$ sudo python -c "import sys;print sys.path" ['',
'/usr/lib/', '/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',

Solution #1:

Did you install flask-sqlalchemy? It looks like you have SQLAlchemy installed but not the Flask extension. Try pip install Flask-SQLAlchemy in your project’s virtualenv to install it from PyPI.

Respondent: D.Shawley

Solution #2:

I just experienced the same problem. Apparently, there is a new distribution method, the extension code is no longer stored under flaskext.

Source: Flask CHANGELOG
This worked for me:

from flask_sqlalchemy import SQLAlchemy
Respondent: sibande

Solution #3:

Install Flask-SQLAlchemy with pip in your virtualenv:

pip install flask_sqlalchemy

Then import flask_sqlalchemy in your code:

from flask_sqlalchemy import SQLAlchemy
Respondent: Ming Liu

Solution #4:

Okay,I have re-installed the package via pip even that didn’t help. And then I rsync’ed the entire /usr/lib/python-2.7 directory from other working machine with similar configuration to
the current machine.It started working. I don’t have any idea ,what was wrong with my setup. I see some difference “print sys.path” output earlier and now. but now my issue is resolved by this work around.

EDIT:Found the real solution for my setup. upgrading “sqlalchemy only doesn’t solve the issue” I also need to upgrade flask-sqlalchemy that resolved the issue.


Solution #5:

try this:

from flask.ext.sqlalchemy import SQLAlchemy
Respondent: mata

Solution #6:

So here is an idea!

Since it seemed to work somewhere else.

install python-virtualenv
and optionally you can install virtualenv-wrapper (which is pretty cool to create projects and so on)

In each env, you might have different versions of eggs. In other word, you could have sqlalchemy 1 and sqlaclhemy 1.5 in two different envs and they won’t conflict with each others. It seems that you have a problem with your currently installed eggs.

So here we go:

virtualenv --no-site-packages foo
source foo/bin/activate

The parameter –no-site-packages will create a virtualenv and not use the packages already installed on your computer. It’s pretty much like a bare python install.

source foo/bin/activate loads the virtualenv.

It’s not that really userfriendly. And that’s why exists.

That said, you should see somthing like thant in your terminal “(foo)[email protected]$:” once your virtualenv is activated. It means that you can go on!

Then you have to do.

python develop of your project. It should download and install dependencies of your project in the virtualenv located in foo. If you need to install anything else, please use pip or easy_install without using sudo. When using virtualenv, you almost never need to use sudo. Sudo will install package in your global python install while it’s not required and not really desirable.

If something happens in your virtualenv, you can always delete it and create a new one. This is no big deal. No need to mess with anything. Doesn’t work? start over, do pip install -U if needed, define the versions if needed and so on.

Last but not least, in the other answers, it seems that the import changed. If the new versions for flask-sqlalchemy is located somewhere else, you should update your import or install the version you used to use.

Solution #7:

first install the library

pip install flask_sqlalchemy 

after that

from flask_sqlalchemy import SQLAlchemy

put this in file to get the access of database through SQLAlchemy

Solution #8:

This code works perfectly:

import sqlalchemy

Maybe you installed the package in another version of the interpreter?

Also, like Shawley pointed out, you need to have the flask extension installed in order for it to be accessible.

Respondent: user1254893

Solution #9:

I’m new comer, use python 3.8 and met the same problem. I installed with pip instead of pip3 because i thought the pip installer is the same for python2 and python3
so this is proper installation

pip3 install flask_sqlalchemy

Respondent: Alexey Nikonov

Solution #10:

Solution for me was to use:

from flask_sqlalchemy import SQLAlchemy

instead of

from flask.ext.sqlalchemy import SQLAlchemy
Respondent: George Mogilevsky

Solution #11:

Very late to the party but hopefully this will help someone, was in the same situation for about a hour without any of the solutions mentioned above working. (On a Windows 10 machine).

In the Settings/Preferences dialog (Ctrl+Alt+S), from the side menu select Project: | Project Interpreter.

Check which packages you currently have installed (You need SQLAlchemy and Flask-SQLAlchemy). Double click on any package name, an ‘Available Packages’ menu will open.

Search for the missing package(s) and click install.

Respondent: RiceCrispy

Solution #12:

On Windows 10 @ 2019

I faced the same problem. Turns out I forgot to install the following package:

pip install flask_sqlalchemy

After installing the package, everything worked perfectly. Hope, it helped some other noob like me.

Respondent: Mujeeb Ishaque

Solution #13:

I just experienced the same problem using the virtual environment.
For me installing the package using python from the venv worked:

.venvenvironmentScriptspython.exe -m pip install flask-sqlalchemy

Respondent: Primakov

Solution #14:

Probably a stupid mistake; but, I experienced this problem and the issue turned out to be that “pip3 install sqlalchemy” installs libraries in user specific directories.

On my Linux machine, I was logged in as user1 executing a python script in user2’s directory. I installed sqlalchemy as user1 and it by default placed the files in user1’s directory. After installing sqlalchemy in user2’s directory the problem went away.

Respondent: Marco Rosas

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy