最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask运行出错:ImportError No module named flask_sqlalchemy

Flask crifan 15862浏览 0评论

参考:

Quickstart — Flask-SQLAlchemy Documentation (2.1)

去写Flask的应用代码:

from flask import Flask, g, request, make_response
from flask import abort, redirect, url_for
# from flask import jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, instance_relative_config=True)
app.debug = app.config[“FLASK_DEBUG”]
app.config.from_object(‘config’) # load config.py
app.config.from_pyfile(‘config.py’)
db = SQLAlchemy(app)
app.logger.debug(“db=%s”, db)

运行出错:

(SIPEvents) ➜  SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 app:app
[2016-08-22 21:16:24 +0000] [6787] [INFO] Starting gunicorn 19.6.0
[2016-08-22 21:16:24 +0000] [6787] [INFO] Listening at: http://127.0.0.1:8080 (6787)
[2016-08-22 21:16:24 +0000] [6787] [INFO] Using worker: sync
[2016-08-22 21:16:24 +0000] [6792] [INFO] Booting worker with pid: 6792
[2016-08-22 21:16:24 +0000] [6794] [INFO] Booting worker with pid: 6794
[2016-08-22 21:16:24 +0000] [6792] [ERROR] Exception in worker process
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/arbiter.py”, line 557, in spawn_worker
    worker.init_process()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/base.py”, line 126, in init_process
    self.load_wsgi()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/base.py”, line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/base.py”, line 67, in wsgi
    self.callable = self.load()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py”, line 65, in load
    return self.load_wsgiapp()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py”, line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/util.py”, line 357, in import_app
    __import__(module)
  File “/usr/share/nginx/html/SIPEvents/app.py”, line 8, in <module>
    from flask_sqlalchemy import SQLAlchemy
ImportError: No module named flask_sqlalchemy

搜:

ImportError: No module named flask_sqlalchemy

python – ImportError: No module named sqlalchemy – Stack Overflow

python – Can’t install flask_sqlalchemy ->ImportError: No module named flask_sqlalchemy – Stack Overflow

说是:

zsh时,从命令行运行,需要先导入:

import sqlalchemy

Flask-SQLAlchemy — Flask-SQLAlchemy Documentation (2.1)

不管了,安装好了:

pip install Flask-SQLAlchemy

(SIPEvents) ➜  SIPEvents pip install Flask-SQLAlchemy
Collecting Flask-SQLAlchemy
  Downloading Flask-SQLAlchemy-2.1.tar.gz (95kB)
    100% |████████████████████████████████| 102kB 33kB/s 
Requirement already satisfied (use –upgrade to upgrade): Flask>=0.10 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask-SQLAlchemy)
Collecting SQLAlchemy>=0.7 (from Flask-SQLAlchemy)
  Downloading SQLAlchemy-1.0.14.tar.gz (4.8MB)
    100% |████████████████████████████████| 4.8MB 47kB/s 
Requirement already satisfied (use –upgrade to upgrade): itsdangerous>=0.21 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy)
Requirement already satisfied (use –upgrade to upgrade): Jinja2>=2.4 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy)
Requirement already satisfied (use –upgrade to upgrade): Werkzeug>=0.7 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy)
Requirement already satisfied (use –upgrade to upgrade): click>=2.0 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.10->Flask-SQLAlchemy)
Requirement already satisfied (use –upgrade to upgrade): MarkupSafe in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Jinja2>=2.4->Flask>=0.10->Flask-SQLAlchemy)
Building wheels for collected packages: Flask-SQLAlchemy, SQLAlchemy
  Running setup.py bdist_wheel for Flask-SQLAlchemy … done
  Stored in directory: /root/.cache/pip/wheels/cf/9f/1b/390c152e645c6e300fda9ed9c678c6e22717a3020fd02acb4d
  Running setup.py bdist_wheel for SQLAlchemy … done
  Stored in directory: /root/.cache/pip/wheels/b6/33/08/eb7224f6053d6abd5bbdd41628505b5af881a6b7828060a6b3
Successfully built Flask-SQLAlchemy SQLAlchemy
Installing collected packages: SQLAlchemy, Flask-SQLAlchemy
Successfully installed Flask-SQLAlchemy-2.1 SQLAlchemy-1.0.14

然后接着去试试

然后就可以了。

[总结]

此处出错:

ImportError: No module named flask_sqlalchemy

的解决办法是:

去安装Flask-SQLAlchemy即可:

pip install Flask-SQLAlchemy

或:

easy_install Flask-SQLAlchemy

转载请注明:在路上 » [已解决]Flask运行出错:ImportError No module named flask_sqlalchemy

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 不可以啊
    Alice7年前 (2018-05-26)回复
92 queries in 0.241 seconds, using 22.18MB memory