I am building a Python 3.11 Flask-SocketIO project into an .exe using PyInstaller on Jenkins (Windows 10). The project works fine when I run it locally with python runapp.py or even inside Jenkins using python runapp.py. But when I run the generated .exe from PyInstaller, I always get:
ValueError: Invalid async_mode specified
I force async_mode="gevent" in my __init__.py:
import gevent.monkey
gevent.monkey.patch_all()
from flask_socketio import SocketIO
socketio = SocketIO(app, cors_allowed_origins="*", async_mode="gevent")
All dependencies are installed (gevent==25.8.2, gevent-websocket==0.10.1, greenlet==3.2.4, Flask-SocketIO==5.3.6, etc). eventlet has been removed to avoid conflicts.
My .spec includes:
hiddenimports = [
'gevent',
'gevent.monkey',
'gevent.threading',
'gevent._socket3',
'gevent._threading',
'gevent._semaphore',
'gevent.builtins',
'geventwebsocket',
'greenlet',
'engineio.async_drivers.threading',
'engineio.async_drivers.gevent',
'engineio.async_drivers.gevent_uwsgi',
'engineio.async_drivers.aiohttp',
'asyncio',
]
When running PyInstaller I also see warnings like:
ERROR: Hidden import 'engineio.async_drivers.asyncio' not found
But I verified that asyncio is available at: C:\Users\Innopharm\AppData\Local\Programs\Python\Python311\Lib\asyncio
Why does this work fine when running python runapp.py, but fails when running the PyInstaller .exe? What am I missing in PyInstaller hidden imports or configuration?
for more additional information the output from the runapp.exe:
Traceback (most recent call last): File "runapp.py", line 2, in <module> File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module File "theone\__init__.py", line 97, in <module> File "flask_socketio\__init__.py", line 187, in __init__ File "flask_socketio\__init__.py", line 243, in init_app File "socketio\base_server.py", line 31, in __init__ File "engineio\base_server.py", line 81, in __init__ ValueError: Invalid async_mode specified [PYI-9784:ERROR] Failed to execute script 'runapp' due to unhandled exception!
I expect the EXE can run
engineio.async_drivers.asyncioand\Lib\asyncioare two different objects. And when you don't addengineio.async_drivers.asynciothen\Lib\asynciomay not work.engineioand if it has.async_drivers.asyncio, not\Lib\asyncio- because it is not the same. it seemsengineiocan mean module python-engineio