【问题标题】:ImportError when starting uwsgi application (flask)启动 uwsgi 应用程序时的 ImportError (flask)
【发布时间】:2015-08-17 23:19:05
【问题描述】:

我有一个使用 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/ 上运行 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/&lt;folder_name&gt; 中放置更多的Flask 项目。

【问题讨论】:

  • 你能展示你的烧瓶应用吗?我想知道'uwsgi_file_brieffenster'..这是从哪里来的?你有那个文件名的导入吗?另外,您使用“不同安装点中的多个烧瓶应用程序”设置的任何原因?你真的有多个烧瓶应用程序在服务器上运行吗?如果您不需要不同的挂载点,有一种更简单的方法来设置 uWSGI。提供更多细节 - 我们会解决这个问题。
  • @GG_Python 我更新了我的帖子,希望能回答你所有的问题:)

标签: nginx flask uwsgi


【解决方案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

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

【讨论】:

    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2014-08-09
    • 2019-04-03
    • 2013-06-27
    • 2021-11-11
    相关资源
    最近更新 更多