0

I'm trying to generate a one-file executable with Pyinstaller and it always fails if I try to add an icon, but when I don't ask my computer to add the icon, the app builds successfully. I've tried following the instructions on the following pages:

https://github.com/PySimpleGUI/PySimpleGUI/issues/445

https://shanetully.com/2013/08/cross-platform-deployment-of-python-applications-with-pyinstaller/

Pyinstaller setting icon

but none of them helped me. Based on the page behind the second of those links, I added the following lines (the latter I added since I'm using Windows, but I couldn't understand what if sys.platform == 'win32' else a.binaries, (mentioned at shanetully's website) is supposed to do):

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')]

to the .spec file and copied the icon to tfolder/images.

The error message I got (and have been getting similar or identical messages with previous attempts as well) was the following. (with my computer username replaced with my stackoverflow username:

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

And yes, I tried googling the last line of the error message on its own, but couldn't find any help that way either.

By the way, this is an app where I actually do want the console to appear when running it.

hilssu
  • 416
  • 4
  • 18
  • did you try with different ico file? – buran May 11 '19 at 08:26
  • Now I tried with this: http://www.iconarchive.com/show/beautiful-flat-icons-by-elegantthemes/document-icon.html and the same error occurred again. – hilssu May 11 '19 at 09:14

1 Answers1

0

EDIT: I had to reset the entire computer and this time installed Python as is and did not use Anaconda like I had previously used. This time Pyinstaller worked fine.

ORIGINAL ANSWER (unedited):

I couldn't get Pyinstaller to work, but there is a workaround. You can compile the program first without the icon (pyinstaller.exe --onefile main.py) on the command line and then use a program called Resource Hacker (http://www.angusj.com/resourcehacker/) to change the icon afterwards.

After changing the icon using Resource Hacker, you may need to move the executable to another place for the icon to appear in the file manager. I moved the executable to my SD card and then the icon appeared, and it appears also when I copy the executable back to my computer's hard drive.

hilssu
  • 416
  • 4
  • 18
  • I also changed the icon twice before moving the executable to my SD card, but I would find it surprising if you had to change the icon twice, just moving the executable would probably have been sufficient. Also, the icon appeared below and in the console window just as intended before moving the exe, only the file manager didn't show the correct icon before. – hilssu May 11 '19 at 13:57