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

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

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

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

  1. 打包完成后,找到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 |

  1. 打包完成后,找到app.exe文件,点击后。exe程序将以黑窗口模式后台运行。
相关推荐
御承扬2 小时前
编程素养提升之EffectivePython(Builder篇)
python·设计模式·1024程序员节
麦麦大数据2 小时前
F032 材料科学文献知识图谱可视化分析系统(四种知识图谱可视化布局) | vue + flask + echarts + d3.js 实现
vue.js·flask·知识图谱·数据可视化·论文文献·1024程序员节·科研图谱
chenchihwen3 小时前
AI代码开发宝库系列:FAISS向量数据库
数据库·人工智能·python·faiss·1024程序员节
AI视觉网奇4 小时前
json 可视化 2025 coco json
python·1024程序员节
mit6.8244 小时前
[nanoGPT] ChatGPT 的 LLM 的全栈实现 | 快速上手
python
DKunYu4 小时前
2.1线性回归
pytorch·python·深度学习·1024程序员节
大飞记Python4 小时前
实战分享:一键自动化下载指定版本的Chrome及Chromedriver(附Python源码)
chrome·python·自动化
麦麦大数据5 小时前
F033 vue+neo4j图书智能问答+知识图谱推荐系统 |知识图谱+neo4j+vue+flask+mysql实现代码
vue.js·flask·nlp·neo4j·智能问答·图书·1024程序员节
程序员杰哥6 小时前
如何使用Postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·python·测试工具·jenkins·postman·1024程序员节
老歌老听老掉牙6 小时前
参数曲线切向量与叉乘向量的精确计算与分析
python·sympy·1024程序员节