Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add image to .spec file in Pyinstaller

Does anybody know how to modify the .spec file created with the Makespec.py of Pyinstaller such that it includes an image data in the _MEIPASS2 Temp dir? I want to be able to add an icon to my exe. I've done what's written here, but I just don't know how to add my data in the .spec.

I'm adding this line in the end of the .spec file:

a.datas += [('iconName.ico','DATA','C:\\Python26\\pyinstaller-1.5.1\\iconName.ico')]
like image 796
maupertius Avatar asked Mar 30 '12 16:03

maupertius


People also ask

How do I add icons to PyInstaller spec?

To change the icon that the .exe has, you can pass the --icon=icon. ico to the pyi-makespec command or modify the . spec file yourself by altering the exe object to add the argument icon='icon. ico' .

How do I use PyInstaller hidden import?

The simpler solution is to use --hidden-import=modulename along with the PyInstaller script. It will add modulename as import statement silently. Hooks are better if you want to specify which import needs what additional modules. --hidden-import is simpler as a one-shot or for debugging.

Which is better py2exe or PyInstaller?

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.


2 Answers

Here is my spec file (Collector.spec) I used for a simple python program called "Collector.py".

# -*- mode: python -*-
a = Analysis(['Collector.py'],
             pathex=['C:\\Users\\vijay\\Python\\Collector'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None)
a.datas += [('logo.png','C:\\Users\\vijay\\System\\icon\\logo.png','DATA')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='Collector.exe',
          debug=False,
          strip=None,
          upx=True,
          console=False , icon='C:\\Users\\vijay\\System\\icon\\logo.ico')

The line "a.datas += .... " just above pyz variable holds the path to png image that will be displayed on various windows of my GUI application. The "icon=...." variable set inside exe variable, holds the path to ico image that will be displayed on Windows Desktop as the Desktop Icon.

You can now use what Max has done here in your main program (Collector.py, for me).

Here is a snippet of my script Collector.py, where I've made use of Max's Code:

path = self.resource_path("logo.png")
icon = wx.Icon(path, wx.BITMAP_TYPE_PNG)
self.SetIcon(icon)

Now, when I run pyinstaller Collector.spec, I have both a Desktop Icon and an Icon for my Collector App windows.

Hope this helps!

like image 193
yvvijay Avatar answered Sep 18 '22 10:09

yvvijay


To add an icon to the executable, you need to use the following:

python Makespec.py --icon=<FILE.ICO> yourprogram.py

Or, you can add the following directly to exe = EXE(...) in the spec file

icon=<FILE.ICO>

This will add the file.ico to the executable's resources (Windows only), and it will show as the application icon. The a.datas append that you are using will add an icon to the internal resources which can be accessed inside the application via sys._meipass. You can use this to add an icon at the top of the main window, for example.

like image 27
Lozzer Avatar answered Sep 18 '22 10:09

Lozzer