Flask-MySQLdb不使用gunicorn和flask

时间:2018-04-03 14:31:30

标签: python flask mysql-python gunicorn

我在virtualenv中使用FlaskMySQL(Ver 14.14 Distrib 5.7.20,osx10.13(x86_64))。如果我使用命令app.py运行以下python3 app.py文件,则一切都运行良好。但是,如果我使用gunicorn使用命令gunicorn --bind 0.0.0.0:8000 app:app来运行它,那么我会收到以下错误。 MySQL和Flask-MySQLdb安装正确,还安装了mysqlclient。

[2018-04-03 19:54:42 +0530] [36173] [INFO] Starting gunicorn 19.7.1
[2018-04-03 19:54:42 +0530] [36173] [INFO] Listening at: http://0.0.0.0:8000 (36173)
[2018-04-03 19:54:42 +0530] [36173] [INFO] Using worker: sync
[2018-04-03 19:54:42 +0530] [36176] [INFO] Booting worker with pid: 36176
[2018-04-03 19:54:42 +0530] [36176] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app
    __import__(module)
  File "/Users/parthapratimneog/Documents/Work/cams-colored-cheque-recognition/colored_cheque_recognition/app.py", line 3, in <module>
    from flask_mysqldb import MySQL
ModuleNotFoundError: No module named 'flask_mysqldb'
[2018-04-03 19:54:42 +0530] [36176] [INFO] Worker exiting (pid: 36176)
[2018-04-03 19:54:43 +0530] [36173] [INFO] Shutting down: Master
[2018-04-03 19:54:43 +0530] [36173] [INFO] Reason: Worker failed to boot.

app.py

import os
from flask import Flask, request, redirect, url_for, send_from_directory, flash, jsonify
from flask_mysqldb import MySQL

from werkzeug.utils import secure_filename

from vision import Cheque

app = Flask(__name__)

ROOT_DIR = app.root_path
UPLOAD_FOLDER = os.path.join(ROOT_DIR, 'uploads')
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'tif'}

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'asdlasj1283798uxXJOaisdj'
app.config['SESSION_TYPE'] = 'filesystem'

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'root'
app.config['MYSQL_DB'] = 'dbname'

mysql = MySQL(app)

if __name__ == '__main__':
    app.run(debug=True)


@app.route('/test',  methods=['GET'])
def test_mysql():
    cur = mysql.connection.cursor()
    cur.execute('''SELECT * FROM bank_details limit 2''')
    rv = cur.fetchall()
    return str(rv)

pip3 freeze output

click==6.7
cycler==0.10.0
decorator==4.2.1
Flask==0.12.2
Flask-MySQLdb==0.2.0
Flask-SQLAlchemy==2.3.2
gunicorn==19.7.1
imutils==0.4.6
itsdangerous==0.24
Jinja2==2.10
kiwisolver==1.0.1
MarkupSafe==1.0
matplotlib==2.2.2
mysqlclient==1.3.12
networkx==2.1
numpy==1.14.2
opencv-python==3.4.0.12
Pillow==5.0.0
PyMySQL==0.8.0
pyparsing==2.2.0
pytesseract==0.2.0
python-dateutil==2.7.2
pytz==2018.3
PyWavelets==0.5.2
scikit-image==0.13.1
scipy==1.0.1
six==1.11.0
SQLAlchemy==1.2.6
Werkzeug==0.14.1

基于gunicorn错误,似乎gunicorn没有在虚拟环境中使用python。如果这是正确的,我如何让gunicorn从虚拟环境中使用python。如果不是,还有什么可能是问题?

修改

在PyCharm内部,我收到了以下警告。 enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我发现了这个问题。老实说,这有点傻。从以前的项目中已经全局安装了gunicorn,并且在virtualenv内也以某种方式安装了全局gunicorn

因此,我卸载了全局gunicorn,然后仅在我的virtualenv内再次安装它,它开始像魅力一样工作。