Search code examples
pythonpython-3.xflaskuwsgi

Flask app has ModuleNotFoundError when using Alpine, nginx, and uwsgi.. but with flask run it works fine


I've written a small flask app, which runs fine on my Windows 10 desktop using flask run, and also runs fine on an Alpine Linux 3.14 Docker container using flask run, but when I use nginx and uwsgi, I receive a ModuleNotFoundError: No module named '_cffi_backend' error. I have run through all the other articles on stackoverflow which fit this description, to no avail.

From python --version I am running Python 3.9.7, which has been installed using the official python:3.9-alpine3.14 container from Docker Hub. I have also added the following apk add --no-cache python3-dev gcc libc-dev libffi-dev uwsgi-python3 jpeg-dev zlib-dev git nano

Here is the full error from the uwsgi log

uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
spawned uWSGI master process (pid: 109)
spawned uWSGI worker 1 (pid: 110, cores: 2)
spawned uWSGI worker 2 (pid: 111, cores: 2)
bcrypt is required to use Flask-Bcrypt
Traceback (most recent call last):
  File "/app/./run.py", line 4, in <module>
    from ticketsapi import create_app
  File "/app/./ticketsapi/__init__.py", line 8, in <module>
    from flask_bcrypt import Bcrypt
  File "/usr/local/lib/python3.9/site-packages/flask_bcrypt.py", line 27, in <module>
    raise e
  File "/usr/local/lib/python3.9/site-packages/flask_bcrypt.py", line 24, in <module>
    import bcrypt
  File "/usr/local/lib/python3.9/site-packages/bcrypt/__init__.py", line 25, in <module>
    from . import _bcrypt  # type: ignore
ModuleNotFoundError: No module named '_cffi_backend'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. GAME OVER ***
SIGINT/SIGQUIT received...killing workers...
Exception ignored in: <module 'threading' from '/usr/lib/python3.9/threading.py'>
Traceback (most recent call last):
  File "/app/./run.py", line 4, in <module>
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 1388, in _shutdown
    from ticketsapi import create_app
  File "/app/./ticketsapi/__init__.py", line 7, in <module>
    from playhouse.flask_utils import FlaskDB
  File "/usr/local/lib/python3.9/site-packages/playhouse/flask_utils.py", line 12, in <module>
    from playhouse.db_url import connect as db_url_connect
  File "/usr/local/lib/python3.9/site-packages/playhouse/db_url.py", line 7, in <module>
    from playhouse.cockroachdb import CockroachDatabase
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load
  File "<frozen importlib._bootstrap>", line 157, in __enter__
  File "<frozen importlib._bootstrap>", line 196, in _get_module_lock
KeyboardInterrupt
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. GAME OVER ***
    def _shutdown():
KeyboardInterrupt:

The output from pip freeze

backports.entry-points-selectable==1.1.0
bcrypt==3.2.0
blinker==1.4
certifi==2021.10.8
cffi==1.15.0
click==8.0.3
cryptography==35.0.0
distlib==0.3.3
dnspython==2.1.0
email-validator==1.1.3
filelock==3.3.1
Flask==2.0.2
Flask-Bcrypt==0.7.1
Flask-Login==0.5.0
Flask-Mail==0.9.1
Flask-WTF==0.15.1
idna==3.3
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
peewee==3.14.4
Pillow==8.4.0
platformdirs==2.4.0
pycparser==2.20
PyMySQL==1.0.2
python-dotenv==0.19.1
six==1.16.0
supervisor==4.2.2
virtualenv==20.9.0
Werkzeug==2.0.2
WTForms==2.3.3

My nginx conf

server {
    listen 5000;

    location /static {
        alias /app/ticketsapi/static;
    }

    location / {
        uwsgi_pass unix:///app/socket/uwsgi.sock;
        include /etc/nginx/uwsgi_params;
        # proxy_redirect off;
    }
}

My uwsgi ini

## Application
##
module = run:app
chdir=/app
socket = /app/socket/uwsgi.sock
chown-socket = nginx:nginx
chmod-socket = 777
processes = 5
threads = 2


## General
##
strict = true
master = true
enable-threads = true
vacuum = true
single-interpreter = true
die-on-term = true
need-app = true
lazy-apps = true

I've been working on resolving this for a couple of days now, and I am stumped. How can I resolve this error and move forward with my project?


Solution

  • By going through the various logs, I noticed that there were references to two slightly different versions of Python, namely 3.9.5 and 3.9.7.

    Although I'm using Alpine Linux 3.14, I used this Digital Ocean tutorial to resolve my issue by employing a Python Virtual Environment (venv).