我正在尝试使用Pyinstaller生成一个文件可执行文件,如果尝试添加图标,它总是失败,但是当我不要求计算机添加图标时,该应用程序将成功构建。我已尝试按照以下页面上的说明进行操作:
https://github.com/PySimpleGUI/PySimpleGUI/issues/445
https://shanetully.com/2013/08/cross-platform-deployment-of-python-applications-with-pyinstaller/
但是他们都没有帮助我。根据这些链接的 second 后面的页面,我添加了以下几行(由于使用Windows,因此添加了第二行,但我不明白if sys.platform == 'win32' else a.binaries,
(在shanetully的网站)应该这样做):
a.datas + [('images/ticon.ico', 'tfolder/images/ticon.ico', 'DATA')]
a.binaries + [('msvcp100.dll', 'C:\\Windows\\System32\\msvcp100.dll', 'BINARY'),
('msvcr100.dll', 'C:\\Windows\\System32\\msvcr100.dll', 'BINARY')]
到.spec文件,然后将图标复制到tfolder/images
。
以下是我得到的错误消息(并且与以前的尝试同样收到相似或相同的消息)。 (将我的计算机用户名替换为我的stackoverflow用户名:
PS C:\Users\hilssu\Documents\pyinstaller_ij> pyinstaller.exe "C:\Users\hilssu\Documents\pyinstaller_ij\main.spec"
103 INFO: PyInstaller: 3.4
103 INFO: Python: 3.5.6
106 INFO: Platform: Windows-10-10.0.17134-SP0
109 INFO: UPX is not available.
112 INFO: Extending PYTHONPATH with paths
['C:\\Users\\hilssu\\Documents\\pyinstaller_ij',
'C:\\Users\\hilssu\\Documents\\pyinstaller_ij']
114 INFO: checking Analysis
297 INFO: checking PYZ
364 INFO: checking PKG
446 INFO: Building because C:\Users\hilssu\Documents\pyinstaller_ij\build\main\main.exe.manifest changed
446 INFO: Building PKG (CArchive) PKG-00.pkg
156467 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
156523 INFO: Bootloader c:\program files\anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
156523 INFO: checking EXE
156525 INFO: Building EXE because EXE-00.toc is non existent
156530 INFO: Building EXE from EXE-00.toc
156537 INFO: SRCPATH [('images\\ticon.ico', None)]
156538 INFO: Updating icons from ['images\\ticon.ico'] to C:\Users\hilssu\AppData\Local\Temp\tmpzh0lbqym
Traceback (most recent call last):
File "c:\program files\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\program files\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Program Files\Anaconda3\Scripts\pyinstaller.exe\__main__.py", line 9, in <module>
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 111, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 838, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 784, in build
exec(text, spec_namespace)
File "<string>", line 35, in <module>
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 433, in __init__
self.__postinit__()
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
self.assemble()
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 522, in assemble
icon.CopyIcons(tmpnm, self.icon)
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\utils\win32\icon.py", line 177, in CopyIcons
return CopyIcons_FromIco(dstpath, [srcpath])
File "c:\program files\anaconda3\lib\site-packages\PyInstaller\utils\win32\icon.py", line 137, in CopyIcons_FromIco
win32api.UpdateResource(hdst, RT_GROUP_ICON, i, data)
File "c:\program files\anaconda3\lib\site-packages\win32ctypes\pywin32\win32api.py", line 269, in UpdateResource
handle, type, name, language, lp_data, len(lp_data))
File "c:\program files\anaconda3\lib\site-packages\win32ctypes\core\cffi\_resource.py", line 129, in _UpdateResource
lpData = ffi.from_buffer(cData)
File "c:\program files\anaconda3\lib\site-packages\cffi\api.py", line 340, in from_buffer
return self._backend.from_buffer(self.BCharA, python_buffer)
TypeError: from_buffer() cannot return the address of the raw string within a bytes or unicode object
是的,我尝试自行搜索错误消息的最后一行,但也找不到这种帮助。
顺便说一句,这是我实际上要在其中运行控制台的应用程序。
答案 0 :(得分:0)
编辑:我不得不重置整个计算机,这次按原样安装了Python,并且不像以前一样使用了Anaconda。这次Pyinstaller运行正常。
原始答案(未编辑)
我无法使Pyinstaller正常工作,但是有一种解决方法。您可以先在命令行上不带图标(pyinstaller.exe --onefile main.py
)的情况下编译程序,然后使用称为Resource Hacker(http://www.angusj.com/resourcehacker/)的程序来更改图标。
使用Resource Hacker更改图标后,您可能需要将可执行文件移动到另一个位置,以便图标出现在文件管理器中。我将可执行文件移到了SD卡上,然后出现了图标,当我将可执行文件复制回计算机的硬盘驱动器时,该图标也出现了。