uWSGI cannot find "application" using Flask and Virtualenv

38,094

Solution 1

unable to find "application" callable in file /var/www/coefficient/flask.py

is the key :)

Your app is defining an 'app' callable, so you have to instruct uWSGI to search for it, instead of 'application'.

You can use the option

callable: app

and it will works (this is explained in official Flask docs)

Solution 2

Alternatively, you can add module = flaskapp:app to your ini.

Also, indeed, callable is addressed in uwsgi-docs more clearly:

Flask exports its WSGI function (the one we called “application” at the beginning of this quickstart) as “app”, so we need to instruct uWSGI to use it: uwsgi --wsgi-file myflaskapp.py --callable app

Share:
38,094

Related videos on Youtube

skyler
Author by

skyler

Android developer at Grooveshark

Updated on September 18, 2022

Comments

  • skyler
    skyler over 1 year

    Using uWSGI to serve a simple wsgi app, (a simple "Hello, World") my configuration works, but when I try to run a Flask app, I get this in uWSGI's error logs:

    current working directory: /opt/python-env/coefficient/lib/python2.6/site-packages
    writing pidfile to /var/run/uwsgi.pid
    detected binary path: /opt/uwsgi/uwsgi
    setuid() to 497
    your memory page size is 4096 bytes
    detected max file descriptor number: 1024
    lock engine: pthread robust mutexes
    uwsgi socket 0 bound to TCP address 127.0.0.1:3031 fd 3
    Python version: 2.6.6 (r266:84292, Jun 18 2012, 14:18:47)  [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)]
    Set PythonHome to /opt/python-env/coefficient/
    *** Python threads support is disabled. You can enable it with --enable-threads ***
    Python main interpreter initialized at 0xbed3b0
    your server socket listen backlog is limited to 100 connections
    *** Operational MODE: single process ***
    added /opt/python-env/coefficient/lib/python2.6/site-packages/ to pythonpath.
    unable to find "application" callable in file /var/www/coefficient/flask.py
    unable to load app 0 (mountpoint='') (callable not found or import error)
    *** no app loaded. going in full dynamic mode ***
    *** uWSGI is running in multiple interpreter mode ***`
    

    Note in particular this part of the log:

    unable to find "application" callable in file /var/www/coefficient/flask.py

    unable to load app 0 (mountpoint='') (callable not found or import error)

    ******no app loaded. going in full dynamic mode******

    This is my Flask app:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello, World, from Flask!"
    

    Before I added my Virtualenv's pythonpath to my configuration file, I was getting an ImportError for Flask. I solved this though, I believe (I'm not receiving errors about it anymore) and here is my complete configuration file:

    uwsgi:
      #socket: /tmp/uwsgi.sock 
      socket: 127.0.0.1:3031
      daemonize: /var/log/uwsgi.log
      pidfile: /var/run/uwsgi.pid
      master: true
      vacuum: true
      #wsgi-file: /var/www/coefficient/coefficient.py
      wsgi-file: /var/www/coefficient/flask.py
      processes: 1
      virtualenv: /opt/python-env/coefficient/
      pythonpath: /opt/python-env/coefficient/lib/python2.6/site-packages
    

    This is how I start uWSGI, from an rc script:

    /opt/uwsgi/uwsgi --yaml /etc/uwsgi/conf.yaml --uid uwsgi

    And if I try to view the Flask program in a browser, I get this:

    **uWSGI Error**
    
    Python application not found
    

    Any help is appreciated.

  • skyler
    skyler almost 12 years
    You're correct: setting callable: app fixed it, and as you said, this in the Flask docs. Strangely, though, on another uWSGI install, using the same Python code, I did not have to specify callable: app; it just worked out of the box.
  • Hamman Samuel
    Hamman Samuel over 7 years
    Where do I set this value of callable: app? In flask.py?