当前位置: 首页 > news >正文

Pyinstaller 打包flask_socketio为exe程序后出现:ValueError: Invalid async_mode specified

 Pyinstaller 打包flask_socketio为exe程序后出现:ValueError: Invalid async_mode specified

一、详细描述问题描述

Traceback (most recent call last):

  File "app_3.py", line 22, in <module>

  File "flask_socketio\__init__.py", line 186, in __init__

  File "flask_socketio\__init__.py", line 242, in init_app

  File "socketio\base_server.py", line 34, in __init__

  File "engineio\base_server.py", line 94, in __init__

ValueError: Invalid async_mode specified

二、解决方法

1.确认flask_socketio的版本flask_socketio version>5.x 则不需要async_mode=‘’参数。使用 pip show flask_socketio 命令查看版本,版本信息如下。

Name: Flask-SocketIO

Version: 5.5.1

可运行的最小基础代码如下,可在该代码验证环境是否正常,index.html为首页可在template文件夹中自行创建。注意allow_unsafe_werkzeug=True,这个配置必须添加。否则会出现错误。

app = Flask(__name__)
app.secret_key = 'your_secret_key'
# socketio = SocketIO(app, async_mode='eventlet') # 5.x版本已废弃该命令
socketio = SocketIO(app)

@app.route('/')
def welcome():
    return render_template('index.html')

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000, debug=True, allow_unsafe_werkzeug=True)

index.html文件如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    <h1>Welcome to My Flask App!</h1>
    <p>This is a simple welcome page.</p>
</body>
</html>

2. 使用pyinstaller命令打包,这一步打包后会生成一个配置参数文件app.spec。注意确保

确保根目录下有templates, static两个文件夹。

 pyinstaller --onefile --noconsole --add-data="templates;templates" --add-data="static;static" app.py

3. 打包完成后,找到app.spec配置文件。完成如下操作:

(1)在相关的行,强制添加如下库的依赖。信息如下。

a = Analysis(
    ['app.py'],
    pathex=[],
    binaries=[],
  hiddenimports=["gevent","geventwebsocket","gevent.ssl","gevent.builtins","engineio.async_drivers.threading"],
    datas=[('templates', 'templates'), ('static', 'static')],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)

(2)修改配置,开启后台运行窗口模型

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='app',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True, #  False -> True
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

(3)删除原来的dist、bulid文件文件,使用 pyinstaller app.spec命令重新打包

pyinstaller app.spec

4. 打包完成后,找到app.exe文件,点击后。exe程序将以黑窗口模式后台运行。

http://www.dtcms.com/a/109074.html

相关文章:

  • 学习threejs,使用Texture纹理贴图,测试repeat重复纹理贴图
  • ngx_regex_init
  • C语言基础要素(019):输出ASCII码表
  • 李沐 X 动手学深度学习--第九章 现代循环神经网络
  • webstorm初始化配置项目
  • MySQL学习集--DDL
  • Python 数据科学实战指南:从零开始构建高效分析流程
  • 单片机学习之SPI
  • JVM深入原理(七)(一):运行时数据区
  • 图形界面设计理念
  • 使用 PyTorch 的 `optim.lr_scheduler.CosineAnnealingLR` 学习率调度器
  • 蓝桥云客-修建灌木
  • Ubuntu环境基于Ollama部署DeepSeek+Open-Webui实现本地部署大模型-无脑部署
  • 静态库和动态库
  • 数据结构复习(单调栈,单调队列,KMP,manacher,tire,字符串哈希)
  • HTML 音频(Audio)学习笔记
  • 时间数据的可视化
  • 什么是msvcp140.dll?msvcp140.dll丢失的解决方法又有哪些?
  • 运算放大器(四)滤波电路(滤波器)
  • Apollo配置中心登陆页面表单密码加密提交
  • 2025年通信安全员 B 证考试真题分享
  • Windows 系统 Git 2.15.0 (64位) 下载与安装教程
  • 使用 PyTorch 的 `GradualWarmupScheduler` 实现学习率预热
  • Java全栈面试宝典:线程协作与Spring Bean管理深度解析
  • echarts 各类统计图 高分辨下文字模糊
  • SpringBoot全局exception处理最佳实践
  • MySQL深入
  • P10744 [SEERC 2020] Modulo Permutations 题解
  • 基于Contiue来阅读open-r1中的GRPO训练代码
  • 【Pandas】pandas DataFrame select_dtypes