78

I get the below error when I try and start Flask using uWSGI. Here is how I start:

>  # cd ..
>     root@localhost:# uwsgi --socket 127.0.0.1:6000 --file /path/to/folder/run.py --callable app -  -processes 2

Here is my directory structure:

-/path/to/folder/run.py
      -|app
          -|__init__.py
          -|views.py
          -|templates
          -|static

Contents of /path/to/folder/run.py

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

Contents of /path/to/folder/app/__init__.py

import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
#from flaskext.babel import Babel
from config import basedir
app = Flask(__name__)
app.config.from_object('config')
#app.config.from_pyfile('babel.cfg')

db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.setup_app(app)
login_manager.login_view = 'login'
login_manager.login_message = u"Please log in to access this page."

from app import views

*** Operational MODE: preforking ***
unable to find "application" callable in file /path/to/folder/run.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 ***
spawned uWSGI worker 1 (pid: 26972, cores: 1)
spawned uWSGI worker 2 (pid: 26973, cores: 1)
akira
  • 6,050
  • 29
  • 37
Tampa
  • 75,446
  • 119
  • 278
  • 425

3 Answers3

203

I had problems with the accepted solution because my flask app was in a variable called app. You can solve that with putting just this in your wsgi:

from module_with_your_flask_app import app as application

So the problem was simply that uwsgi expects a variable called application.

Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • 46
    THIS should be published in bold letter, 100-point font in the docs! – ankush981 Feb 21 '16 at 07:17
  • 24
    you can also use `callable = app` in the `ini` file or use the `--callable app` flag if you're invoking `uwsgi` directly – Matt Jun 15 '16 at 05:06
  • Grrrr, this was exactly the problem. I second the notion that this should be in 64px font in the beginning of the examples :) It makes no sense that it only detects ``application.run()`` and nothing else, even if it is correct by syntax. – Juha Untinen Aug 26 '17 at 16:50
  • 1
    heh, first one to submit a pull request to add this to the docs gets a cookie (I make good cookies) – Milimetric Sep 11 '17 at 15:19
  • This was particularly useful for gevent and psycogreen early patching with the flask application factory configuration via ```create_app()``` within ```__init__.py```. application = application... Who would have guessed the uWSGI docs quickstart was being so literal... – Tim Pozza Feb 12 '20 at 22:19
53

uWSGI doesn't load your app as __main__, so it never will find the app (since that only gets loaded when the app is run as name __main__). Thus, you need to import it outside of the if __name__ == "__main__": block.

Really simple change:

from app import app as application  # for example, should be app

if __name__ == "__main__":
    application.run()

Now you can run the app directly with python run.py or run it through uWSGI the way you have it.

NOTE: if you set --callable myapp, you'd need to change it from as application to myapp (by default uwsgi expects application

Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
  • 1
    This really helped me - I'm reusing someone else's code which uses Flask to wrap a RESTful api around a service I wanted and I couldn't for the the life of me figure out why it worked fine **unless** run under wsgi. It had a number of key parameters set under if __name__ == "__main__" which I moved to the main section of the code, leaving just the app.run() statement behind, and now works fine. – Chaffelson Aug 05 '15 at 12:52
  • 5
    This is not enough, uwsgi expects application not app, thus: else: application = app – stelios Jan 21 '17 at 19:04
  • @Jeff Tratner: Please consider changing the accepted answer (given the comment by stelios. – B--rian Jan 27 '20 at 12:20
  • If you look at the initial question, the user has listed `--callable app` which is why this is appropriate. Generally I guess default is `application`. Perhaps we should edit both question and answer. – Jeff Tratner Feb 16 '20 at 05:27
2

The uWSGI error unable to load app 0 (mountpoint='') (callable not found or import error) occured for me if I left out the last two lines of the following minimal working example for Flask application

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello world!"

if __name__ == "__main__":
    app.run()
else:
    application = app

I am aware that this already implicitly said within the comments to another answer, but it still took me a while to figure that out, so I hope to save others' time.

In the case of a pure Python Dash application, I can offer the following minimal viable code snippet:

import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div( html.H1(children="Hello World") )

application = app.server

if __name__ == "__main__":
    app.run_server(debug=True)

Again, the application = app.server is the essential part here.

B--rian
  • 5,578
  • 10
  • 38
  • 89