接上文将Python文件转化为exe文件,若生成EXE文件报如下错误:
E:\Program\Python\test1>pyinstaller -F main.py -i 1.ico -w
82 INFO: PyInstaller: 5.7.0
82 INFO: Python: 3.10.1
95 INFO: Platform: Windows-10-10.0.22000-SP0
96 INFO: wrote E:\Program\Python\test1\main.spec
98 INFO: UPX is not available.
99 INFO: Extending PYTHONPATH with paths
['E:\\Program\\Python\\test1']
309 INFO: checking Analysis
396 INFO: checking PYZ
407 INFO: checking PKG
483 INFO: Bootloader D:\Soft\Python310\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
483 INFO: checking EXE
490 INFO: Rebuilding EXE-00.toc because main.exe missing
490 INFO: Building EXE from EXE-00.toc
490 INFO: Copying bootloader EXE to E:\Program\Python\test1\dist\main.exe.notanexecutable
494 INFO: Copying icon to EXE
Traceback (most recent call last):
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\icon.py", line 50, in normalize_icon_type
from PIL import Image as PILImage
ModuleNotFoundError: No module named 'PIL'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Soft\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\Soft\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "D:\Soft\Python310\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "D:\Soft\Python310\lib\site-packages\PyInstaller\__main__.py", line 194, in _console_script_run
run()
File "D:\Soft\Python310\lib\site-packages\PyInstaller\__main__.py", line 180, in run
run_build(pyi_config, spec_file, **vars(args))
File "D:\Soft\Python310\lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\build_main.py", line 971, in main
build(specfile, distpath, workpath, clean_build)
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\build_main.py", line 893, in build
exec(code, spec_namespace)
File "E:\Program\Python\test1\main.spec", line 24, in <module>
exe = EXE(
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\api.py", line 552, in __init__
self.__postinit__()
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\datastruct.py", line 173, in __postinit__
self.assemble()
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\api.py", line 657, in assemble
icon.CopyIcons(build_name, self.icon)
File "D:\Soft\Python310\lib\site-packages\PyInstaller\utils\win32\icon.py", line 203, in CopyIcons
srcpath = normalize_icon_type(srcpath, ("exe", "ico"), "ico", config.CONF["workpath"])
File "D:\Soft\Python310\lib\site-packages\PyInstaller\building\icon.py", line 54, in normalize_icon_type
raise ValueError(
ValueError: Received icon image 'E:\Program\Python\test1\1.ico' which exists but is not in the correct format. On this platform, only ('exe', 'ico') images may be used as icons. If Pillow is installed, automatic conversion will be attempted. Please install Pillow or convert your 'ico' file to one of ('exe', 'ico') and try again.
我们不妨看看报错信息
raise ValueError(
ValueError: Received icon image 'E:\Program\Python\test1\1.ico'
which exists but is not in the correct format. On this platform,
only ('exe', 'ico') images may be used as icons. If Pillow is installed,
automatic conversion will be attempted.
Please install Pillow or convert your 'ico' file to one of ('exe', 'ico') and try again.
这里提示的是你的ICO图标文件格式,可能出现问题。
细致的来说,你不能直接简单的把PNG/JPG格式的图标改变为ICO扩展名。
解决方法如下:
推荐使用在线的一个ICO转换器:
ICO在线转换器
打开界面如下:
下载后,再次重新输入:
E:\Program\Python\test1>pyinstaller -F main.py -i 1.ico -w
68 INFO: PyInstaller: 5.7.0
68 INFO: Python: 3.10.1
81 INFO: Platform: Windows-10-10.0.22000-SP0
81 INFO: wrote E:\Program\Python\test1\main.spec
83 INFO: UPX is not available.
83 INFO: Extending PYTHONPATH with paths
['E:\\Program\\Python\\test1']
277 INFO: checking Analysis
371 INFO: checking PYZ
383 INFO: checking PKG
454 INFO: Bootloader D:\Soft\Python310\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
455 INFO: checking EXE
456 INFO: Rebuilding EXE-00.toc because main.exe missing
456 INFO: Building EXE from EXE-00.toc
456 INFO: Copying bootloader EXE to E:\Program\Python\test1\dist\main.exe.notanexecutable
456 INFO: Copying icon to EXE
456 INFO: Copying icons from ['E:\\Program\\Python\\test1\\1.ico']
456 INFO: Writing RT_GROUP_ICON 0 resource with 132 bytes
456 INFO: Writing RT_ICON 1 resource with 1128 bytes
456 INFO: Writing RT_ICON 2 resource with 2440 bytes
456 INFO: Writing RT_ICON 3 resource with 4264 bytes
456 INFO: Writing RT_ICON 4 resource with 9640 bytes
456 INFO: Writing RT_ICON 5 resource with 16936 bytes
456 INFO: Writing RT_ICON 6 resource with 21640 bytes
456 INFO: Writing RT_ICON 7 resource with 38056 bytes
456 INFO: Writing RT_ICON 8 resource with 67624 bytes
456 INFO: Writing RT_ICON 9 resource with 58800 bytes
471 INFO: Copying 0 resources to EXE
472 INFO: Embedding manifest in EXE
472 INFO: Updating manifest in E:\Program\Python\test1\dist\main.exe.notanexecutable
473 INFO: Updating resource type 24 name 1 language 0
473 INFO: Appending PKG archive to EXE
473 INFO: Fixing EXE headers
554 INFO: Building EXE from EXE-00.toc completed successfully.
就会看到Building成功啦。
于是在你py文件的目录下 dist文件夹中就会生成一个.exe文件。