?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__) |
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( |
(2)修改配置,開啟后臺運行窗口模型
exe = EXE( |
(3)刪除原來的dist、bulid文件文件,使用 pyinstaller app.spec命令重新打包。
pyinstaller app.spec |
4.?打包完成后,找到app.exe文件,點擊后。exe程序將以黑窗口模式后臺運行。