【解决方法】Docker+uWSGI+Flask 报错 ModuleNotFoundError: No module named ‘flask‘

news/2025/2/26 13:31:39

背景

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 呢?

还望大佬不吝赐教。

以上。

祝大家变的更强。


http://www.niftyadmin.cn/n/4411677.html

相关文章

【解决方法】如何压缩网页字体文件

背景 一个小的 H5 项目&#xff0c;客户要求使用指定的字体&#xff0c;一般来讲如果有特殊字体&#xff0c;前端这边都按照切图来处理了。 但是这个项目中的特殊字体的字符内容是变化的&#xff0c;也就没办法提前切图。 所以只能在网页中使用字体文件&#xff0c;又因为文…

【实现一套爬虫数据抓取平台】[3-5-03] 微博长短地址转换

文章目录零、系列目录一、背景二、代码三、总结零、系列目录 写这套文章的时候&#xff0c;不会完全按照目录的顺序一篇一篇写&#xff0c; 大家可以到目录中直接找到对应的章节进行查看。 点我跳转 一、背景 新浪微博有两类地址&#xff0c;分别是&#xff1a; # pc站地址…

【刷题】589. N 叉树的前序遍历

题目地址 点击跳转 题目描述 给定一个 n 叉树的根节点 root &#xff0c;返回 其节点值的 前序遍历 。 n 叉树 在输入中按层序遍历进行序列化表示&#xff0c;每组子节点由空值 null 分隔&#xff08;请参见示例&#xff09;。 示例 1&#xff1a; 输入&#xff1a;root…

【刷题】NC337 IP地址转化

题目地址 点击跳转 题目描述 IP地址是一个用 ‘.’ 隔开的四段数字&#xff0c;每段的大小是 0 ≤ n ≤ 255 。请你把 IP 地址转换成一个整数。(IPv4) 例如&#xff0c; 114.55.207.244 的二进制表示是 01110010 00110111 11001111 11110100 &#xff0c;其十进制表示是 759…

Struts生成Word下载

比较巧的是&#xff0c;刚做完一个下载Excel文件的功能&#xff0c;又有一个新的问题&#xff0c;就是需要将一份文件导出到word中&#xff0c;刚开始我找到的方法&#xff0c;无非是将网页中内容用wrod打开&#xff0c;但是网页的版式也复制了进去&#xff0c;显的很不正规。相…

Oracle 9i Enterprise Manager Console 无法启动的解决方法

前几天心血来潮重新拾起它。于是隔壁同学那借来三张oracle 9i的盘。安装过程一切顺利。接着兴冲冲打开Enterprise Manager Console&#xff0c;发现窗口在屏幕上闪了下就没了&#xff0c;启动不起来&#xff01;我试着又去启动SQLPlus Worksheet发现情况也是一样的。~~~超级郁闷…

oracle trigger代码

sql 代码--[6]// Oracle Trigger --实例1------------------------ --创建触发器&#xff0c;当用户对test表执行DML语句时&#xff0c;将相关信息记录到日志表 --创建测试表 CREATE TABLE test ( t_id NUMBER(4), t_name VARCHAR2(20), t_age NUMBER(2), t_sex CHAR ); --创建…