ImportError:virtualenv中没有名为flask.ext.sqlalchemy的模块

时间:2017-05-15 06:08:42

标签: python flask sqlalchemy

以下是尝试在flask_sqlalchemy import SQLAlchemy中运行python3中的以下代码时遇到的主要错误

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'

不确定它是否相关但是当我更新sqlaclhemy时它会提供以下点数。我尝试更新pip,但出于某种原因,版本9.0.1表示它已升级但实际上并未更新。

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.

这是我运行pip list时得到的结果,因为你可以看到sqlalchemy和flask-sqlalchemy都已安装。

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)

我猜它使用的是不正确的Python版本。我从来没有深究过这个问题,但是我的虚拟机还没有修好它。感谢帮助人员,希望我能解决它。

4 个答案:

答案 0 :(得分:15)

导入声明:

from flask.ext.sqlalchemy import SQLAlchemy

引发异常ImportError: No module named flask.ext.sqlalchemy in virtualenv,因为扩展代码不再存储在flask.ext下,如this answer中所述。

因此,import语句应更改为:

from flask_sqlalchemy import SQLAlchemy

答案 1 :(得分:2)

代替:

  from flask.ext.mongoalchemy import MongoAlchemy

使用:

  from flask_mongoalchemy import MongoAlchemy

答案 2 :(得分:1)

试试这个 pip install flask-sqlalchemy

答案 3 :(得分:0)

我有同样的问题,我正在使用Flask-WhooshAlchemy(0.56)扩展名,但是如果您尝试导入它: <div align="${shoudSetAlign ? myObject.memberVariable'someString' : ''}" </div> 给出了报告的错误:"${shoudSetAlign ? myObject.memberVariable'someString' : ''}"

然后,我将Flask-WhooshAlchemy(第18行)中的import语句从import flask_whooshalchemy as whooshalchemy更改为 到ImportError: No module named flask.ext.sqlalchemy

这解决了导入错误,但这意味着您具有Flask-WhooshAlchemy的修改版本。