启动 uwsgi 应用程序(烧瓶)时出现 ImportError

ImportError when starting uwsgi application (flask)

我有一个使用 Python3 和 Flask 的应用程序,我想使用 virtualenv.

uwsginginx 上部署它
# Brieffenster
location /projekte/brieffenster {
    include uwsgi_params;
    uwsgi_pass unix:///tmp/brieffenster.sock;
}

[uwsgi]
plugin = python3

socket = /tmp/brieffenster.sock
chown-socket = www-data:www-data

chdir = /var/www/projekte/brieffenster
virtualenv = /var/www/projekte/brieffenster/.env
mount = /projekte/brieffenster=brieffenster.py
callable = app
manage-script-name = true

运行 http://<SERVER_IP>/projekte/brieffenster/ returns 502 Bad Gateway.

上的 HTTP GET

当我使用

从命令行启动它时
sudo uwsgi --ini /etc/uwsgi/apps-enabled/brieffenster.ini

有一个堆栈跟踪(如下所示)。我已经调试了几个小时,我认为我做的一切都是对的。

有什么我遗漏的吗?

$ sudo uwsgi --ini /etc/uwsgi/apps-enabled/brieffenster.ini
[uWSGI] getting INI configuration from /etc/uwsgi/apps-enabled/brieffenster.ini
*** Starting uWSGI 1.9.17.1-debian (64bit) on [Mon Aug 17 23:13:01 2015] ***
compiled with version: 4.8.2 on 23 March 2014 17:15:32
os: Linux-2.6.32-042stab094.7 #1 SMP Wed Oct 22 12:43:21 MSK 2014
nodename: bender
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /
detected binary path: /usr/bin/uwsgi-core
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 514091
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/brieffenster.sock fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.4.0 (default, Jun 19 2015, 14:24:19)  [GCC 4.8.2]
Set PythonHome to /var/www/projekte/brieffenster/.env
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x106f720
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72792 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
mounting brieffenster.py on /projekte/brieffenster
Traceback (most recent call last):
  File "/usr/lib/python3.4/pkgutil.py", line 481, in find_loader
    spec = importlib.util.find_spec(fullname)
  File "/var/www/projekte/brieffenster/.env/lib/python3.4/importlib/util.py", line 100, in find_spec
    raise ValueError('{}.__spec__ is None'.format(name))
ValueError: uwsgi_file_brieffenster.__spec__ is None

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "brieffenster.py", line 28, in <module>
    app = CustomFlask(__name__)
  File "/var/www/projekte/brieffenster/.env/lib/python3.4/site-packages/flask/app.py", line 331, in __init__
    instance_path = self.auto_find_instance_path()
  File "/var/www/projekte/brieffenster/.env/lib/python3.4/site-packages/flask/app.py", line 622, in auto_find_instance_path
    prefix, package_path = find_package(self.import_name)
  File "/var/www/projekte/brieffenster/.env/lib/python3.4/site-packages/flask/helpers.py", line 661, in find_package
    loader = pkgutil.get_loader(root_mod_name)
  File "/usr/lib/python3.4/pkgutil.py", line 467, in get_loader
    return find_loader(fullname)
  File "/usr/lib/python3.4/pkgutil.py", line 487, in find_loader
    raise ImportError(msg.format(fullname, type(ex), ex)) from ex
ImportError: Error while finding loader for 'uwsgi_file_brieffenster' (<class 'ValueError'>: uwsgi_file_brieffenster.__spec__ is None)

更新 1: 这是我正在使用的应用程序。它只是一个包罗万象的东西,告诉我请求了哪条路径。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from flask import Flask

__author__ = 'Jonas Gröger <jonas.groeger@gmail.com>'


app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return 'You want path: %s' % path

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

我正在使用 mountmanage-script-name,因为我计划将更多 Flask 项目放入 /projekte/<folder_name>

这是一个通用应用程序 - 似乎不需要不同的安装点和专门的设置。这是我的 uWSGI 设置以适应您的设置(在括号中填写缺少的参数)。我认为您只是缺少 module 参数。 master=true 将消除您收到的警告。

uWSGI ini 文件:

[uwsgi]
module = [your flask app filename from update 1, WITHOUT '.py']
callable = app
master = true
processes = 5
socket = /tmp/brieffenster.sock
enable-threads = true
uid = www-data
gid = www-data
vacuum = true
venv = /var/www/projekte/brieffenster/.env
die-on-term = true

看看这是否有效;如果不是- post 进一步指导的错误信息。