简体   繁体   中英

Flask-MySQLdb not working with gunicorn and flask

I am using Flask and MySQL (Ver 14.14 Distrib 5.7.20, for osx10.13 (x86_64)) inside a virtualenv. If I run the following app.py file using the command python3 app.py , everything runs perfectly. But, if I use gunicorn to run it using the command gunicorn --bind 0.0.0.0:8000 app:app , then I get the following error. MySQL and Flask-MySQLdb are installed properly, mysqlclient is also installed.

[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

Based on the gunicorn error, it seems like gunicorn is not using the python inside the virtual environment. If that is correct, how do I make gunicorn use the python from the virtual environment. If no, what else might be the issue?

EDIT

Inside PyCharm, I am getting the following warning though. 在此处输入图片说明

Ok, I found the issue. It's kind of silly to be honest. There was already gunicorn installed globally from previous projects, and somehow inside the virtualenv also, that global gunicorn was getting called.

So, I uninstalled the global gunicorn and then installed it again only inside my virtualenv , and it started working like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM