天天看點

【解決方法】Docker+uWSGI+Flask 報錯 ModuleNotFoundError: No module named ‘flask‘背景問題分析解決方案其他問題

背景

Docker

+

Nginx

+

uWSGI

+

Flask

部署的環境,以前一直都能好好跑,這次把基礎鏡像的

Python

版本由原來的

3.6

更新到了

3.8

,就報了标題的錯。

Docker

+

Nginx

+

uWSGI

+

Flask

部署可以參考 這篇文章

問題分析

先看一下 Docker 的啟動日志:

Starting nginx: nginx.,
*** Starting uWSGI 2.0.18-debian (64bit) on [Tue Aug 17 02:21:46 2021] ***,
[uWSGI] getting INI configuration from uwsgi.ini,
compiled with version: 8.2.0 on 10 February 2019 02:42:46,
os: Linux-3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018,
nodename: 9c8cc3ffd4ed,
machine: x86_64,
pcre jit disabled,
detected number of CPU cores: 2,
clock source: unix,
current working directory: /code,
detected binary path: /usr/bin/uwsgi-core,
uWSGI running as root, you can use --uid/--gid/--chroot options,
chdir() to /code,
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** ,
*** WARNING: you are running uWSGI without its master process manager ***,
your memory page size is 4096 bytes,
detected max file descriptor number: 1048576,
lock engine: pthread robust mutexes,
thunder lock: disabled (you can enable it with --thunder-lock),
uwsgi socket 0 bound to TCP address :5000 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.7.3 (default, Jan 22 2021, 20:04:44)  [GCC 8.3.0],
Python main interpreter initialized at 0x55fa4f5a8990,
uWSGI running as root, you can use --uid/--gid/--chroot options,
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** ,
python threads support enabled,
your server socket listen backlog is limited to 100 connections,
your mercy for graceful operations on workers is 60 seconds,
mapped 825016 bytes (805 KB) for 8 cores,
*** Operational MODE: preforking+threaded ***,
Traceback (most recent call last):,
  File "run.py", line 20, in <module>,
    from server import create_app,
  File "./server/__init__.py", line 14, in <module>,
    from flask import Flask,
unable to load app 0 (mountpoint='') (callable not found or import error),
*** no app loaded. going in full dynamic mode ***,
uWSGI running as root, you can use --uid/--gid/--chroot options,
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** ,
*** uWSGI is running in multiple interpreter mode ***,
spawned uWSGI worker 1 (pid: 21, cores: 2),
spawned uWSGI worker 2 (pid: 22, cores: 2),
spawned uWSGI worker 3 (pid: 23, cores: 2),
spawned uWSGI worker 4 (pid: 24, cores: 2)
           

從中可以看出來,就是在引用

flask

的時候報錯了。

那什麼原因會導緻這個問題呢?

就是程式在跑起來的時候沒有找到 python 庫(flask),就報錯了。

這裡咱們先不管他為什麼沒找到,既然沒找到的話,那我們就主動告訴他去哪找就行了。

解決方案

修改

uwsgi.ini

,設定

pythonpath

的值為

/usr/local/lib/python3.8/site-packages/

修改後的

uwsgi.ini

檔案内容為:

[uwsgi]
chdir = /code
socket = :5000
pythonpath = /usr/local/lib/python3.8/site-packages/
wsgi-file = run.py
callable = app
chmod-socket = 666
plugins = python3
buffer-size = 65535
processes = 4
threads = 2
           

好了,問題解決。

其他問題

好了,解決了能跑的問題,還有一個比較重要的問題,為什麼不能跑呢?

之前在

Python3.6

的容器裡面都能直接跑,為什麼在

Python3.8

的容器裡面就需要指定

PYTHONPATH

呢?

還望大佬不吝賜教。

以上。

祝大家變的更強。