Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uwsgi with Flask app gives "callable not found or import error"

I want to serve a basic Flask app in a virtualenv using Python 3 and nginx. I'm getting the error

Internal Server Error

when I attempt to browse to the page. I also see errors in /var/log/uwsgi/app/myproj.log that lead me to believe that the error lies within my uwsgi config file. nginx and uwsgi seem to be communicating just fine.

Here's my directory structure:

/srv/http/myproj/
             |----- setup.py
             |----- env/
             |----- myproj/
                       |----- __init__.py
                       |----- myproj.py
/etc/uwsgi/apps-enabled/
                 |----- myproj.ini
/etc/nginx/sites-enabled/
                 |----- myproj

Here's the error I see in /var/log/uwsgi/app/myproj.log:

Thu Jun  8 00:00:41 2017 - *** Operational MODE: preforking ***
Thu Jun  8 00:00:41 2017 - unable to load app 0 (mountpoint='') (callable not found or import error)
Thu Jun  8 00:00:41 2017 - *** no app loaded. going in full dynamic mode ***
Thu Jun  8 00:00:41 2017 - *** uWSGI is running in multiple interpreter mode ***
Thu Jun  8 00:00:41 2017 - spawned uWSGI master process (pid: 14498)
Thu Jun  8 00:00:41 2017 - spawned uWSGI worker 1 (pid: 14504, cores: 1)
Thu Jun  8 00:00:41 2017 - spawned uWSGI worker 2 (pid: 14505, cores: 1)
Thu Jun  8 00:00:43 2017 - --- no python application found, check your startup logs for errors ---
[pid: 14505|app: -1|req: -1/1] 172.16.72.2 () {46 vars in 726 bytes} [Thu Jun  8 00:00:43 2017] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

Here's /etc/uwsgi/apps-enabled/myproj.ini:

[uwsgi]
plugins = python3
venv = /srv/http/myproj/env
chdir = /srv/http/myproj
module = myproj:myproj
callable = app

I also tried with module set to myproj (which made no change in the uwsgi logs) and myproj.myproj (which was less successful because it couldn't find the module myproj.myproj).

Here's /srv/http/myproj/myproj/myproj.py:

import flask
app = flask.Flask(__name__)

Here's /srv/http/myproj/myproj/__init__.py:

from myproj.myproj import app

Here's /etc/nginx/sites-enabled/myproj:

upstream myproj {
        server unix:///run/uwsgi/app/myproj/socket fail_timeout=0;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name _;

        location / {
                uwsgi_pass myproj;
                include /etc/nginx/uwsgi_params;
        }
}

I suppose my question is simple: What am I doing wrong?

Edit: In case it matters:

# lsb_release -d
Description:    Ubuntu 16.04.2 LTS
like image 304
Todd Avatar asked Nov 25 '25 11:11

Todd


1 Answers

I found the solution after looking at a similarly structured project. Here's the new file that solved the problem:

[uwsgi]
plugins = python3
venv = /srv/http/myproj/env
chdir = /srv/http/myproj/myproj
pythonpath = ..
module = myproj
callable = app

Essentially, I needed my chdir to go one level deeper (and therefore the module one level less deep).

Edit: And finally, to get my imports in my project to work properly, I also needed to add the pythonpath line seen above.

like image 123
Todd Avatar answered Nov 27 '25 00:11

Todd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!