Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named flask.ext.sqlalchemy in virtualenv

Here is the main error I get when trying to run the following code in python3 from flask_sqlalchemy import SQLAlchemy

Traceback (most recent call last):
  File "/home/jsnyder10/.local/bin/flask", line 11, in <module>
sys.exit(main())
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 513, in main
cli.main(args=args, prog_name=name)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 380, in main
return AppGroup.main(self, *args, **kwargs)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 423, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 152, in __init__
self._load_unlocked()
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 176, in _load_unlocked
self._app = rv = self.loader()
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 237, in load_app
rv = locate_app(self.app_import_path)
  File "/home/jsnyder10/.local/lib/python3.5/site-packages/flask/cli.py", line 90, in locate_app
__import__(module)
  File "/home/jsnyder10/Documents/CS50/pset7/pset7/finance/application.py", line 2, in <module>
from flask_sqlalchemy import SQLAlchemy
ImportError: No module named 'flask_sqlalchemy'

Not sure if it is related but when I update sqlaclhemy it gives the following pip heads up. I tried updating pip, but for some reason version 9.0.1 says it is upgraded but doesn't actually update.

You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Here is what I get when I run pip list, as you can see both sqlalchemy and flask-sqlalchemy are installed.

DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
appdirs (1.4.3)
click (6.7)
Flask (0.12.1)
Flask-SQLAlchemy (2.2)
itsdangerous (0.24)
Jinja2 (2.9.6)
MarkupSafe (1.0)
packaging (16.8)
pip (9.0.1)
pyparsing (2.2.0)
setuptools (35.0.2)
six (1.10.0)
SQLAlchemy (1.1.9)
Werkzeug (0.12.1)
wheel (0.29.0)

I'm guessing it was using the incorrect Python version. I never got to the bottom of this, but nuking my virtual machine and making a new one fixed it. Thanks for the help guys, wish I could have fixed it.

like image 817
rockets4all Avatar asked May 15 '17 06:05

rockets4all


People also ask

How do you fix No module named flask?

The Python "ModuleNotFoundError: No module named 'flask'" occurs when we forget to install the Flask module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install Flask command.

How do I know if Sqlalchemy is installed flask?

To check which version of sqlalchemy is installed, use pip show sqlalchemy or pip3 show sqlalchemy in your CMD/Powershell (Windows), or terminal (macOS/Linux/Ubuntu) to obtain the output major.

How would you upgrade a package named flask?

X version then all you need to do is just install flas module with this command: pip3 install flask. Second solution is First of all just create a new virtualenv with this command: virtualenv flask Then open it with: cd flask. Now you have to activate the virtualenv with this command: source bin/activate.


2 Answers

The import statement:

from flask.ext.sqlalchemy import SQLAlchemy

raises the exception ImportError: No module named flask.ext.sqlalchemy in virtualenv since the extension code is no longer stored under flask.ext, as stated in this answer.

Therefore, the import statement should be changed to:

from flask_sqlalchemy import SQLAlchemy
like image 90
Liyan Song Avatar answered Oct 19 '22 12:10

Liyan Song


I have the same problem, I'm using Flask-WhooshAlchemy (0.56) extension, but if you try to import it: import flask_whooshalchemy as whooshalchemy that gives the reported error: ImportError: No module named flask.ext.sqlalchemy.

Then I have changed the import statement in Flask-WhooshAlchemy (line 18) from import flask.ext.sqlalchemy as flask_sqlalchemy to import flask_sqlalchemy as flask_sqlalchemy # Modified.

That solve the import error, but that means that you have a modified version of Flask-WhooshAlchemy.

like image 44
Juanu Avatar answered Oct 19 '22 14:10

Juanu