一、问题复现
错误代码
Traceback (most recent call last):
File "D:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "D:\Program Files\Python39\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\__main__.py", line 194, in _console_script_run
run()
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\__main__.py", line 180, in run
run_build(pyi_config, spec_file, **vars(args))
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 1019, in main
build(specfile, distpath, workpath, clean_build)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 944, in build
exec(code, spec_namespace)
File "D:\pycharm项目\打地鼠\打地鼠.spec", line 7, in <module>
a = Analysis(
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 429, in __init__
self.__postinit__()
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
self.assemble()
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 596, in assemble
self.graph.process_post_graph_hooks(self)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\depend\analysis.py", line 329, in process_post_graph_hooks
module_hook.post_graph(analysis)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\depend\imphook.py", line 450, in post_graph
self._process_hidden_imports()
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\depend\imphook.py", line 506, in _process_hidden_imports
self.module_graph.import_hook(import_module_name, caller)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1444, in import_hook
submodule = self._safe_import_module(head, mname, submodule)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1992, in _safe_import_module
self._process_imports(n)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2811, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\depend\analysis.py", line 432, in _safe_import_hook
return super()._safe_import_hook(target_module_partname, source_module, target_attr_names, level, edge_attr)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2262, in _safe_import_hook
target_modules = self.import_hook(
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1431, in import_hook
target_package, target_module_partname = self._find_head_package(
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1610, in _find_head_package
target_package = self._safe_import_module(
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\depend\analysis.py", line 469, in _safe_import_module
hook_module.pre_safe_import_module(hook_api)
File "D:\Program Files\Python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\pre_safe_import_module\hook-win32com.py", line 30, in pre_safe_import_module
win32com_file = exec_statement(
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 99, in exec_statement
return __exec_statement(statement, capture_stdout=True)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 79, in __exec_statement
return __exec_python_cmd(cmd, capture_stdout=capture_stdout)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 70, in __exec_python_cmd
txt = compat.exec_python(*cmd, env=pp_env)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\compat.py", line 553, in exec_python
return exec_command(*cmdargs, **kwargs)
File "D:\Program Files\Python39\lib\site-packages\PyInstaller\compat.py", line 357, in exec_command
out = out.decode(encoding)
AttributeError: 'str' object has no attribute 'decode'
错误图
二、解决步骤
下面的博客解决了问题:
https://blog.csdn.net/weixin_42081389/article/details/100114126
就是在最后一行:File"D:\Program Files\Python39\lib\site-packages\PyInstaller\compat.py", line 357, in exec_command
out = out.decode(encoding)
在compat.py原先的基础上增加个条件,做个判断,如果是字符串类型的,直接跳过,如果是其他的还和之前代码功能一样。
三、成功运行
再次打包:
PS D:\pycharm项目\打地鼠> pyinstaller -F -w 打地鼠.py
570 INFO: PyInstaller: 5.13.0
572 INFO: Python: 3.9.9
591 INFO: Platform: Windows-10-10.0.22621-SP0
593 INFO: wrote D:\pycharm项目\打地鼠\打地鼠.spec
601 INFO: Extending PYTHONPATH with paths
['D:\\pycharm项目\\打地鼠']
pygame 2.5.0 (SDL 2.28.0, Python 3.9.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
1463 INFO: checking Analysis
1464 INFO: Building Analysis because Analysis-00.toc is non existent
1464 INFO: Initializing module dependency graph...
1468 INFO: Caching module graph hooks...
1496 INFO: Analyzing base_library.zip ...
3728 INFO: Loading module hook 'hook-encodings.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
5598 INFO: Loading module hook 'hook-pickle.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
6771 INFO: Loading module hook 'hook-heapq.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7686 INFO: Caching module dependency graph...
7873 INFO: running Analysis Analysis-00.toc
7878 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by D:\Program Files\Python39\python.exe
8048 INFO: Analyzing D:\pycharm项目\打地鼠\打地鼠.py
8287 INFO: Loading module hook 'hook-PIL.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
8390 INFO: Loading module hook 'hook-PIL.Image.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
9427 INFO: Loading module hook 'hook-numpy.py' from 'D:\\Program Files\\Python39\\Lib\\site-packages\\numpy\\_pyinstaller'...
10862 INFO: Loading module hook 'hook-difflib.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
11285 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
11498 INFO: Loading module hook 'hook-xml.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
12466 INFO: Loading module hook 'hook-platform.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
12839 INFO: Loading module hook 'hook-sysconfig.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
14582 INFO: Loading module hook 'hook-packaging.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
14641 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
14682 INFO: Processing module hooks...
14817 INFO: Loading module hook 'hook-pkg_resources.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
16867 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
16967 INFO: Processing pre-safe import module hook win32com from 'D:\\Program Files\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
17209 INFO: Processing pre-safe import module hook distutils from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-distutils.py'.
17212 INFO: Processing pre-find module path hook distutils from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
17257 INFO: Loading module hook 'hook-distutils.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
17278 INFO: Loading module hook 'hook-distutils.util.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
17801 INFO: Loading module hook 'hook-_tkinter.py' from 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
17804 INFO: checking Tree
17804 INFO: Building Tree because Tree-00.toc is non existent
17805 INFO: Building Tree Tree-00.toc
18016 INFO: checking Tree
18017 INFO: Building Tree because Tree-01.toc is non existent
18017 INFO: Building Tree Tree-01.toc
18038 INFO: checking Tree
18039 INFO: Building Tree because Tree-02.toc is non existent
18039 INFO: Building Tree Tree-02.toc
18094 INFO: Looking for ctypes DLLs
18108 INFO: Analyzing run-time hooks ...
18115 INFO: Including run-time hook 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
18126 INFO: Including run-time hook 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
18134 INFO: Including run-time hook 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
18140 INFO: Including run-time hook 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
18145 INFO: Including run-time hook 'D:\\Program Files\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
18228 INFO: Looking for dynamic libraries
D:\Program Files\Python39\lib\site-packages\PyInstaller\building\build_main.py:167: UserWarning: The numpy.array_api submodule is still experimental. See NEP 47.
__import__(package)
1014 INFO: Extra DLL search directories (AddDllDirectory): ['D:\\Program Files\\Python39\\lib\\site-packages\\numpy\\.libs']
1015 INFO: Extra DLL search directories (PATH): ['D:\\Program Files\\Python39\\Scripts\\', 'D:\\Program Files\\Python39\\', 'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\Sy
stem32\\Wbem', 'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\WINDOWS\\System32\\OpenSSH\\', 'C:\\Recovery\\OEM\\Backup\\', 'C:\\Users\\Admin\\AppData\\Local\\pip\\cache\
\wheels\\04\\5f\\3e\\46cc37c5d698415694d83f607f833f83f0149e49b3af9d0f38', 'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\WINDOWS\\System32\\WindowsPowe
rShell\\v1.0\\', 'C:\\WINDOWS\\System32\\OpenSSH\\', 'D:\\Program Files\\wkhtmltopdf\\bin', 'D:\\ffmpeg-6.1.1-essentials_build\\bin', 'D:\\果果编程\\python3.9.9', 'D:\\果果编程\\py
thon3.9.9\\Scripts', '%PyCharm Community Edition%', 'D:\\果果编程\\python3.9.9\\Lib\\site-packages', 'D:\\果果编程\\python3.9.9\\lib\\site-packages', 'C:\\Users\\Admin\\AppData\\Local\\Microsoft\\WindowsApps', 'D:\\Program Files\\wkhtmltopdf\\bin', 'D:\\ffmpeg-6.1.1-essentials_build\\bin']
21347 INFO: Looking for eggs
21370 INFO: Using Python library D:\Program Files\Python39\python39.dll
21370 INFO: Found binding redirects:
[]
21383 INFO: Warnings written to D:\pycharm项目\打地鼠\build\打地鼠\warn-打地鼠.txt
21504 INFO: Graph cross-reference written to D:\pycharm项目\打地鼠\build\打地鼠\xref-打地鼠.html
21586 INFO: checking PYZ
36142 INFO: Writing RT_ICON 6 resource with 4264 bytes
36694 INFO: Embedding manifest in EXE
36695 INFO: Updating manifest in D:\pycharm项目\打地鼠\dist\打地鼠.exe.notanexecutable
36696 INFO: Updating resource type 24 name 1 language 0
37244 INFO: Appending PKG archive to EXE
37278 INFO: Fixing EXE headers
37608 INFO: Building EXE from EXE-00.toc completed successfully.