带有 Flask 应用程序的 uwsgi 提供 "callable not found or import error"

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

我想使用 Python 3 和 nginx 在 vi​​rtualenv 中提供基本的 Flask 应用程序。我收到错误

Internal Server Error

当我尝试浏览页面时。我还在 /var/log/uwsgi/app/myproj.log 中看到错误,这让我相信错误出在我的 uwsgi 配置文件中。 nginx 和 uwsgi 似乎可以正常通信。

这是我的目录结构:

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

这是我在 /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)

这是/etc/uwsgi/apps-enabled/myproj.ini

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

我还尝试将 module 设置为 myproj(这在 uwsgi 日志中没有任何变化)和 myproj.myproj(不太成功,因为它找不到模块myproj.myproj).

这是/srv/http/myproj/myproj/myproj.py

import flask
app = flask.Flask(__name__)

这是/srv/http/myproj/myproj/__init__.py

from myproj.myproj import app

这是/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;
        }
}

我想我的问题很简单:我做错了什么?

编辑:以防万一:

# lsb_release -d
Description:    Ubuntu 16.04.2 LTS

我在查看了类似结构的项目后找到了解决方案。这是解决问题的新文件:

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

基本上,我需要我的 chdir 更深一层(因此模块更深一层)。

编辑:最后,为了让我在项目中的导入正常工作,我还需要添加上面看到的 pythonpath 行。

请检查 uwsgi 进程是否具有 wsgi.py

的读取权限