Search code examples
pythonnginxflaskuwsgilinode

Flask with UWSGI: Python application not found


I have followed the Linode guide on setting up the UWSGI with Flask application, and everything seems OK until I try to put my app instead of the sample one, when I get the "uWSGI Error Python application not found".

The structure of my files in /srv/www/xxx.com/xxx/ is:

app/
    __init__.py
uwsgi.py

The __init__.py file has the line for running the app:

if __name__ == '__main__':
    app.run()

And the uwsgi.py imports the app:

#!flask/bin/python

from app import app

if __name__ == "__main__":
    app.run(debug = False)

When I manually try to run the python uwsgi.py the app seems to work, so no errors or wrong imports. If I change the contents of uwsgi.py to something like this (from example):

import os
import sys

sys.path.append('/srv/www/example.com/application')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/example.com/.python-egg'

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

The app seems to run, so the UWSGI and nginx are working correctly and running the uwsgi.py file, but not when I point it to my app.

My uwsgi config:

<uwsgi>
    <plugin>python</plugin>
    <socket>/run/uwsgi/app/xxx.com/xxx.com.socket</socket>
    <pythonpath>/srv/www/xxx.com/application/xxx/</pythonpath>
    <app mountpoint="/">

        <script>uwsgi</script>

    </app>
    <master/>
    <processes>4</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <no-orphans/>
    <vacuum/>
</uwsgi>

My nginx config:

server {
        listen          80;
        server_name     $hostname;
        access_log /srv/www/xxx.com/logs/access.log;
        error_log /srv/www/xxx.com/logs/error.log;

        location / {
            uwsgi_pass      unix:///run/uwsgi/app/xxx.com/xxx.com.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;

        }

        location /static {
            root   /srv/www/xxx.com/public_html/static/;
            index  index.html index.htm;

        }

}

And this is the uwsgi error log:

added /srv/www/xxx.com/application/xxx/ to pythonpath.
- 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 master process (pid: 21982)
- spawned uWSGI worker 1 (pid: 21990, cores: 1)
- set cpu affinity for worker 1 toTue Sep 23 16:51:19 2014 -  0Tue Sep 23 16:51:19 2014 -
- spawned uWSGI worker 2 (pid: 21991, cores: 1)
- *** Stats server enabled on /tmp/stats.socket fd: 14 ***
- set cpu affinity for worker 2 toTue Sep 23 16:51:19 2014 -  0Tue Sep 23 16:51:19 2014 -

Anyone can point me to where I am making mistakes? I spent hours trying various solutions but nothing seem to work, I must have omitted something important but can't locate it.


Solution

  • Try adding <callable>application</callable>