使用auto-py-to-exe打包Flet计数器应用为EXE时遇错求助
解决Flet应用打包为EXE时的PyInstaller/auto-py-to-exe错误
问题重现
使用Flet编写的计数器应用代码如下:
import sys sys.setrecursionlimit(sys.getrecursionlimit() * 5) import flet as ft def main(page: ft.Page): page.title = "Flet counter example" page.vertical_alignment = ft.MainAxisAlignment.CENTER txt_number = ft.TextField(value="0", text_align=ft.TextAlign.RIGHT, width=100) def minus_click(e): txt_number.value = str(int(txt_number.value) - 1) page.update() def plus_click(e): txt_number.value = str(int(txt_number.value) + 1) page.update() page.add( ft.Row( [ ft.IconButton(ft.icons.REMOVE, on_click=minus_click), txt_number, ft.IconButton(ft.icons.ADD, on_click=plus_click), ], alignment=ft.MainAxisAlignment.CENTER, ) ) ft.app(main)
使用auto-py-to-exe或flet pack打包时出现如下错误:
An error occurred while packaging Traceback (most recent call last): File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 372, in call return isolated.call(function, *args, **kwargs) File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 293, in call ok, output = loads(b64decode(self._read_handle.readline())) EOFError: EOF read where object expected During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Lenovo\anaconda3\lib\site-packages\auto_py_to_exe\packaging.py", line 131, in package run_pyinstaller() File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 180, in run run_build(pyi_config, spec_file, **vars(args)) File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 61, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 1006, in main build(specfile, distpath, workpath, clean_build) File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 928, in build exec(code, spec_namespace) File "C:\Users\Lenovo\AppData\Local\Temp\tmprfg02jem\main.spec", line 7, in <module> a = Analysis( File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 428, in __init__ self.__postinit__() File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__ self.assemble() File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 736, in assemble isolated.call(find_binary_dependencies, self.binaries, self.binding_redirects, collected_packages) File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 372, in call return isolated.call(function, *args, **kwargs) File "C:\Users\Lenovo\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 247, in __exit__ self._write_handle.flush() OSError: [Errno 22] Invalid argument Project output will not be moved to output folder Complete.
解决方案
1. 升级核心依赖包
这类错误多数是版本兼容问题,先把相关工具包升级到最新版:
pip install --upgrade pyinstaller auto-py-to-exe flet
2. 调整Flet启动参数
修改代码中ft.app(main)的调用方式,指定视图模式或端口,避免默认webview的打包冲突:
# 可选方案1:指定桌面视图 ft.app(main, view=ft.AppView.FLET_APP) # 可选方案2:指定浏览器视图并设置端口 ft.app(main, view=ft.AppView.WEB_BROWSER, port=8550)
3. 禁用PyInstaller隔离模式
- 如果用auto-py-to-exe:在高级选项中勾选Disable PyInstaller Isolated Mode
- 如果用命令行PyInstaller:添加
--no-isolated参数
pyinstaller --onefile --windowed main.py --no-isolated
4. 用干净的虚拟环境打包
Anaconda环境可能有冗余依赖干扰,创建纯净虚拟环境重新打包:
# 用venv创建虚拟环境 python -m venv flet_pack_env # 激活环境(Windows) flet_pack_env\Scripts\activate # 安装必要依赖 pip install flet pyinstaller # 打包 pyinstaller --onefile main.py
5. 手动修改spec文件(终极方案)
如果以上方法无效,手动生成并修改spec文件:
- 生成初始spec文件:
pyinstaller main.py
- 打开生成的
main.spec,在Analysis部分添加exclude_binaries=True:
a = Analysis( ['main.py'], pathex=[], binaries=[], datas=[], hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=None, noarchive=False, exclude_binaries=True # 添加这一行 )
- 用修改后的spec文件打包:
pyinstaller main.spec
内容的提问来源于stack exchange,提问作者emrec
相关产品推荐
相关产品推荐

