启动uwsgi应用程序时的ImportError(flask)

时间:2015-08-17 23:19:05

标签: nginx flask uwsgi

我有一个使用Python3和Flask的应用,想要使用uwsginginxvirtualenv部署它。

# 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/上运行HTTP GET会返回502 Bad Gateway

当我使用

从命令行启动它时
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,因为我计划在/projekte/<folder_name>中添加更多Flask项目。

1 个答案:

答案 0 :(得分:1)

这是一个通用的应用程序 - 似乎不需要不同的挂载点和专门的设置。这是我的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

看看这是否有效;如果不是 - 发布错误消息以获得进一步的指导。