Gunicorn with Flask using wrong Python

20,468

Solution 1

The gunicorn utility may be running out of the system path rather than your virtualenv.

Make sure to pip install gunicorn into the virtualenv.

Here's the pip freeze of a virtualenv I setup to run your app:

(so_2)20:38:25 ~/code/tmp/flask_so$ pip freeze
Flask==0.10.1
Flask-SQLAlchemy==1.0
Jinja2==2.7.1
MarkupSafe==0.18
SQLAlchemy==0.8.2
Werkzeug==0.9.4
gunicorn==18.0
itsdangerous==0.23
wsgiref==0.1.2

In reality, I only ran these pip installs:

$ pip install flask
$ pip install gunicorn
$ pip install Flask-SQLAlchemy

Solution 2

I have the same problem as You. The problem is that gunicorn for some reason load the enviroment outside your virtual env. I solved by uninstalling the package gunicorn outside virtual enviroment;

(env) $ deactivate
$ sudo pip uninstall gunicorn

So you come back to your env and try to run. In my case env folder I typed:

$ source env/bin/activate
(env) $ pip install gunicorn
(env) $ gunicorn server:app
2013-10-19 20:40:56 [11923] [INFO] Starting gunicorn 18.0
2013-10-19 20:40:56 [11923] [INFO] Listening at: http://127.0.0.1:8000 (11923)
2013-10-19 20:40:56 [11923] [INFO] Using worker: sync
2013-10-19 20:40:56 [11926] [INFO] Booting worker with pid: 11926

Solution 3

Gunicorn may be installed at multiple location in your system. It may be present in

  1. OS default Python Path
  2. Anaconda Python Path

By default when you specify

gunicorn -w 4 -b 127.0.0.1:5000 flaskApp:app

You are referrng to operating system's default Python where in the same path flask package is not installed results in error. Better specify which gunicorn you are reffering to by providing proper path to gunicorn

/home/sunil/anaconda2/bin/gunicorn -w 4 -b 127.0.0.1:5000 flaskApp:app

Solution 4

Assuming your virtual environment is called env and your app is called app and gunicorn was install properly.You may try:

sudo env/bin/gunicorn --bind 0.0.0.0:5432 wsgi:app

This force the app to use the gunicron in your virtual environment.

Share:
20,468
Brian Peterson
Author by

Brian Peterson

My toolbox: Python, Django & Flask, HTML/CSS, JQuery, Backbone, PostgreSQL/MySQL, Nginx, Debian

Updated on July 11, 2022

Comments

  • Brian Peterson
    Brian Peterson almost 2 years

    I'm trying to bootstrap a Flask app on a Gunicorn server. By putting the two tools' docs together, plus searching around on SO, this is what I have so far... but it's not quite working.

    app.py:

    from flask import Flask, render_template
    from flask.ext.sqlalchemy import SQLAlchemy
    from werkzeug.contrib.fixers import ProxyFix
    
    app = Flask(__name__)
    app.wsgi_app = ProxyFix(app.wsgi_app)
    db = SQLAlchemy(app)
    
    @app.route('/')
    def index():
        return render_template('index.html')
    

    what I ran:

    From the same directory as app.py,

    gunicorn app:app
    

    Even starting this small, I've missed something. The error message is not very helpful:

    2013-09-12 20:13:07 [11461] [INFO] Starting gunicorn 0.14.5
    2013-09-12 20:13:07 [11461] [INFO] Listening at: http://127.0.0.1:8000 (11461)
    2013-09-12 20:13:07 [11461] [INFO] Using worker: sync
    2013-09-12 20:13:07 [11528] [INFO] Booting worker with pid: 11528
    2013-09-12 20:13:07 [11528] [INFO] Worker exiting (pid: 11528)
    2013-09-12 20:13:08 [11461] [INFO] Shutting down: Master
    2013-09-12 20:13:08 [11461] [INFO] Reason: Worker failed to boot.

    By the way, I'm running this on a Debian Linux system. Many thanks in advance for your help!

    Update

    After turning on debugging, I got some more instructive error messages. This has become a very specific problem very fast: ImportError: No module named flask. Usually I get this sort of error when I'm not using my virtualenv--but I am. And upon closer inspection, Gunicorn seems to be using a different version of Python than my virtualenv uses, that is Python3. So... my particular python seems not to be getting used. How do I fix this, and tell Gunicorn to use the right Python?

    • Kyle Kelley
      Kyle Kelley over 10 years
      How did you install gunicorn?
    • Brian Peterson
      Brian Peterson over 10 years
      sudo apt-get install gunicorn, if I recall.
    • Kyle Kelley
      Kyle Kelley over 10 years
      Ah, can you try installing it into your virtualenv? It's probably loading gunicorn from the system path. Just pip install gunicorn.
    • Brian Peterson
      Brian Peterson over 10 years
      Sure, I'll try that. Hold on.
  • Brian Peterson
    Brian Peterson over 10 years
    Sorry, I kind of redacted my app.py to just show you the basic functionality I was trying to get. Somehow left out the import for SqlAlchemy.
  • Brian Peterson
    Brian Peterson over 10 years
    But, hmm, OK. I think I will try to pip install gunicorn (as the commenter above suggests).
  • Kyle Kelley
    Kyle Kelley over 10 years
    Oh ok. I'll trim my answer to just have the bit about gunicorn being in the virtualenv rather than the system path. Since I was the commenter, my feelings won't be hurt. ;)
  • Brian Peterson
    Brian Peterson over 10 years
    Ah, got it working! I apt-get removed gunicorn globally, pip installed it in my virtualenv, restarted the virtualenv, and it worked like a charm. Thanks much.
  • Kyle Kelley
    Kyle Kelley over 10 years
    Yay! Internet high-fives!
  • Brian Peterson
    Brian Peterson over 10 years
    This is basically what I came to as well--installing gunicorn globally made it not behave well inside the virtualenv.
  • Abhi
    Abhi over 5 years
    This solves the issue. Gunicorn for some unknown reasons loads the gloab env and not the Venv.