Jonas Gröger
Jonas Gröger

Reputation: 1628

ImportError when starting uwsgi application (flask)

I have an app using Python3 and Flask and wanted to deploy it on uwsgi and nginx using a virtualenv.

# Brieffenster
location /projekte/brieffenster {
    include uwsgi_params;
    uwsgi_pass unix:///tmp/brieffenster.sock;
}

and

[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

Running a HTTP GET on http://<SERVER_IP>/projekte/brieffenster/ returns 502 Bad Gateway.

When I launch it from the command line using

sudo uwsgi --ini /etc/uwsgi/apps-enabled/brieffenster.ini

there is a stacktrace (seen below). I have already debugged this for some hours and I think I'm doing everything right.

Is there something I'm missing?

$ 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)

UPDATE 1: Here is the application I am using. It is just a catchall that tells me which path was requested.

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


from flask import Flask

__author__ = 'Jonas Gröger <[email protected]>'


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')

I am using mount and manage-script-name because I plan on putting more Flask projects in /projekte/<folder_name>.

Upvotes: 0

Views: 252

Answers (1)

GG_Python
GG_Python

Reputation: 3541

This is a generic application- there seems to be no need for different mountpoints and a specialized setup. Here's my uWSGI setup adapted to your settings (fill in missing parameters in brackets). I think you're just missing the module parameter. master=true will get rid of the warning you're getting.

uWSGI ini file:

[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

See if this works; if not- post the error message for further guidance.

Upvotes: 1

Related Questions