You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包PySide6代码遇UnicodeDecodeError编码错误求助

打包PySide6代码为exe时遇到UnicodeDecodeError编码问题

代码文件main.py内容

from PySide6.QtWidgets import (QApplication)
if __name__ == '__main__':
    pass

使用的打包命令

pyinstaller main.py

报错信息

14358 INFO: Loading module hook 'hook-distutils.util.py' from
'C:\python-3.10.9-embed-amd64\Lib\site-packages\PyInstaller\hooks'...
15296 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from
'C:\python-3.10.9-embed-amd64\Lib\site-packages\PyInstaller\hooks'...
15749 INFO: Loading module hook 'hook-PySide6.QtCore.py' from 'C:\python-3.10.9-embed-amd64\Lib\site-packages\PyInstaller\hooks'...
16155 INFO: Loading module hook 'hook-PySide6.QtGui.py' from 'C:\python-3.10.9-embed-amd64\Lib\site-packages\PyInstaller\hooks'...
16640 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'C:\python-3.10.9-embed-amd64\Lib\site-packages\PyInstaller\hooks'...
Traceback (most recent call last):
File "tokenize.py", line 332, in find_cookie
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x91 in position 8: invalid start byte

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "runpy.py", line 196, in _run_module_as_main
      File "runpy.py", line 86, in _run_code
      File "C:\python-3.10.9-embed-amd64\Scripts\pyinstaller.exe\__main__.py",

line 7, in
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller_main_.py",
line 194, in console_script_run
run()
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller_main
.py",
line 180, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller_main_.py",
line 61, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\building\build_main.py", line 971, in main
build(specfile, distpath, workpath, clean_build)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\building\build_main.py", line 893, in build
exec(code, spec_namespace)
File "C:\Users\Moon\OneDrive\文档\工作\code\python\main.spec", line 7, in
a = Analysis(
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\building\build_main.py", line 411, in init
self.postinit()
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\building\datastruct.py", line 173, in postinit
self.assemble()
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\building\build_main.py", line 580, in assemble
self.graph.process_post_graph_hooks(self)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\depend\analysis.py", line 330, in process_post_graph_hooks
module_hook.post_graph(analysis)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\depend\imphook.py",
line 454, in post_graph
self._process_hidden_imports()
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\depend\imphook.py",
line 510, in _process_hidden_imports
self.module_graph.import_hook(import_module_name, caller)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\lib\modulegraph\modulegraph.py",
line 1518, in import_hook
submodule = self._safe_import_module(head, mname, submodule)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\depend\analysis.py", line 429, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\lib\modulegraph\modulegraph.py",
line 2053, in _safe_import_module
(module, co) = self._load_module(module_name, pathname, loader)
File "C:\python-3.10.9-embed-amd64\Lib/site-packages\PyInstaller\lib\modulegraph\modulegraph.py",
line 2143, in _load_module
src = loader.get_source(partname)
File "", line 940, in get_source
File "", line 711, in decode_source
File "tokenize.py", line 373, in detect_encoding
File "tokenize.py", line 337, in find_cookie

环境说明

  • Windows 10系统
  • Python 3.10
  • PyInstaller 5.79
  • Mac系统下可正常打包,仅Windows下出现该问题,推测为编码问题

解决方案

  1. 调整文件路径与编码

    • 将代码文件夹移动到无中文、无空格的路径下,比如C:\code\python\,避免OneDrive同步路径带来的编码冲突
    • 用文本编辑器(如VS Code)打开main.py和生成的spec文件,确认编码为UTF-8,若不是则转换为UTF-8后保存
  2. 指定打包编码参数

    • 执行打包命令时强制指定UTF-8编码:
      pyinstaller --encoding=utf-8 main.py
      
  3. 替换Python版本

    • 当前使用的是Python嵌入版,缺少部分标准库组件,建议卸载嵌入版,安装Python 3.10完整安装包,重新安装PyInstaller和PySide6后再尝试打包
  4. 更新PyInstaller

    • 升级到最新稳定版PyInstaller,修复已知编码相关bug:
      pip install --upgrade pyinstaller
      

内容的提问来源于stack exchange,提问作者Frank Mouzrt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 03:08:08