使用Auto-py-to-exe打包含Multiprocessing的Python程序遇'extra'环境错误
问题描述
尝试将运行server.py的Python应用通过单目录模式的Auto-py-to-exe打包为.exe,生成的PyInstaller命令如下:
pyinstaller --noconfirm --onedir --console --icon "path" --name "name" --add-data "path" --add-data "path" --add-data "path" --hidden-import "openpyxl.cell._writer " --exclude-module "gevent server.py" "C:/Users/140476/Documents/57956_EXE_ANALISIS_DATO_TALADRADO/server.py"
环境版本信息
6067924 INFO: PyInstaller: 6.0.0 6067938 INFO: Python: 3.9.13 (conda) 6067956 INFO: Platform: Windows-10-10.0.19045-SP0 Running auto-py-to-exe v2.40.0
打包错误日志
6258721 INFO: Loading module hook 'hook-gevent.py' from File "C:\ProgramData\Anaconda3\lib\site-packages\packaging\markers.py", line 215, in _get_env raise UndefinedEnvironmentName( packaging.markers.UndefinedEnvironmentName: 'extra' does not exist in evaluation environment. Project output will not be moved to output folder Complete.
此前项目打包正常,添加使用Multiprocessing库的PySimpleGUI文件浏览器后出现该问题,控制台运行程序无异常。相关代码片段:
if __name__ == '__main__': if sys.platform.startswith('win'): # On Windows calling this function is necessary. freeze_support()
def interact_callbacks(self): @callback( Output('add-automatically-button', 'children'), Input('add-automatically-button', 'n_clicks') ) def update_output(n_clicks): freeze_support() if n_clicks is not None: print('before process') p = Process(target=startInteraction) p.start() return 'Clicked!' else: return 'Add automatically'
解决方案
- 修正
--exclude-module参数格式:原命令中--exclude-module "gevent server.py"写法错误,需拆分模块,改为--exclude-module gevent(入口脚本server.py无需排除)。错误参数格式会导致PyInstaller解析模块时触发hook-gevent.py的异常。 - 升级
packaging库:执行pip install --upgrade packaging,新版本修复了环境变量extra解析的bug。若升级后仍有问题,可手动找到Anaconda环境下的Lib\site-packages\PyInstaller\hooks\hook-gevent.py,注释或删除其中涉及extra环境变量的标记代码。 - 清理冗余的
freeze_support()调用:仅在if __name__ == '__main__':块中调用该方法即可,删除回调函数内的重复调用,避免打包时进程逻辑混乱。 - 添加PySimpleGUI依赖收集参数:在打包命令中加入
--collect-all pysimplegui,确保PySimpleGUI的所有组件被完整打包。 - 清理打包缓存:删除生成的
build、dist目录及*.spec文件,重新执行打包,避免缓存导致的异常。
内容的提问来源于stack exchange,提问作者yeray
相关产品推荐
相关产品推荐

