Search code examples
pythonflaskuwsgi

Why my python file returns unable to load app in uwsgi flask?


I have read the below questions related to uwsgi and interestingly enough nothing has helped so far:

It seems that I need to import app as application as uWSGI tries to find the application variable.

from tf_api import create_app as application

if __name__ == "__main__":
    application = application()
    application.run()
else:
    print("---------------------->", __name__)
    application = application()

When I run the below command:

uwsgi --socket 0.0.0.0:80 --protocol=tcp -w wsgi:app

It goes to else part and outputs:

----------------------> wsgi
2020-02-20 14:25:22,168 [tf_core] Initialized! [testing=False]
2020-02-20 14:25:22,176 [tf_api] {"msg": "Initialized! Testing: False", "logger_name": "tf_api", "log_level": "DEBUG", "timestamp": "2020-02-20T14:25:22.176664"}
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 27143, cores: 1)

Some logs are printed from the application then it prints no app loaded!

When I put application.run(host='0.0.0.0') in else, it seems that application starts running but on flask default port 5000 not on uWSGI:

----------------------> wsgi
2020-02-20 14:36:57,333 [tf_core] Initialized! [testing=False]
2020-02-20 14:36:57,339 [tf_api] {"msg": "Initialized! Testing: False", "logger_name": "tf_api", "log_level": "DEBUG", "timestamp": "2020-02-20T14:36:57.339723"}
 * Serving Flask app "tf_api" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
2020-02-20 14:36:57,405 [werkzeug]  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
2020-02-20 14:36:57,406 [werkzeug]  * Restarting with stat
unable to load configuration from /usr/local/lib/python3.6/dist-packages/tf_api/uwsgi

BUT it gives an error related to unable to load configuration from. created_app returns app. What do you guys think is the reason for not finding the app in uswgi?


Solution

  • When in uwsgi command I used -w wsgi:app I had to rename below code:

    application = application()
    

    to:

    app = application()
    

    Without app.run(), as uWSGI itself runs the flask application and it is not needed.