4

My icon file myicon.ico in same directory with setup.py. When I run py2exe, myproject.exe doesn't have icon. I looked for solution but couldn't find.

setup.py code is:

from distutils.core import setup
import py2exe

setup(
   windows=[{
         "script": "myproject.py",
         "icon_resources": [(0, "favicon.ico")],
      }]
)

OS: Win8.1 64bit

furkantokac
  • 311
  • 4
  • 17
  • Last I had a problem like this (ages ago, 32bit), I ended up using an external tool to change the icon. Extra points for creating a link in your installer and changing the icon on the link itself rather than on executable. – Dima Tisnek Mar 12 '15 at 13:57

5 Answers5

8

It appears py2exe has a 4-year-old bug on handling icons, but due to its description, I managed to make this workaround:

setup_dict = dict(
    windows = [{'script': "script.py",
                "icon_resources": [(1, "icon.ico")}],
)

setup(**setup_dict)
setup(**setup_dict)

This pretty much builds the project twice. If your project is complex and takes too long to process through py2exe, you can use this to build a dummy py file:

import tempfile
tf = tempfile.NamedTemporaryFile(delete=False)
tf.close()
setup(
    windows = [{
        'script': tf.name,
        "icon_resources":[(1, "icon.ico")]}]
)
os.remove(tf.name)

Just don't forget to set excludes like your project, otherwise you will get your dist folder cluttered with unwanted files.

Ronan Paixão
  • 8,297
  • 1
  • 31
  • 27
  • 1
    This worked for me. The first call to `setup()` does not result in an icon but all the subsequent ones do. So, the dummy `.py` file needs to be used on the first call to `setup()`. Similar solution has also been reported [here](https://sourceforge.net/p/py2exe/mailman/message/34169215/). – crayzeewulf Jun 13 '16 at 21:57
  • Excellent! This fixes the problem for me. The embedded icon files wouldn't render above the 48x48 layer, but now they do. – Pod Jan 25 '18 at 11:39
3

Please try this

from distutils.core import setup

setup(
    options = {'py2exe': {'bundle_files': 1}},
    zipfile = None,
    windows = [{
            "script":"myproject.py",
            "icon_resources": [(1, "favicon.ico")],
            }],
)
jatinkumar patel
  • 2,920
  • 21
  • 28
  • When I try this, it says "bundle-files 1 is not yet supported on win64". If I delete "options = {'py2exe': {'bundle_files': 1}},", icon doesn't change. If I use 32bit system to be able to use bundle_files:1, does it work in 64bit systems ? Or do you have another recommend ? Thanks for answer. – furkantokac Mar 12 '15 at 11:58
  • I had same issue once for 64. But i forget it. I will find it in my old project code. You can vote up for 32-bit windows. – jatinkumar patel Mar 12 '15 at 12:15
  • I have just tried the code in 32bit system, it works without any bundle_files:1 problem but icon didn't change! Is problem may be about icon file ? I tried 256x256 & 64x64 .ico images. – furkantokac Mar 12 '15 at 13:13
2

I handled my problem like that;

  1. I used the above code which I post in my question.
  2. Then I installed Resourch Hacker program.
  3. I opened myprogram.exe file with Resourch Hacker program.
  4. Then Action > Replace Icon > I choosed the icon which I want.
  5. And its ok!

For Resourch Hacker tutorial CLICK THIS

furkantokac
  • 311
  • 4
  • 17
0

I meet the same problem. I have solved it by download a win7 icon from http://www.iconarchive.com/search?q=windows+7&page=5, and the reason should be the icon file that could not work at the very start is not a correct win7 format icon.

this web page https://www.creativefreedom.co.uk/icon-designers-blog/windows-7-icon-sizes/ tells us a topic "Testing your Windows 7 Icon" to check whether a icon is a really win7 icon.

0

I used a different program, came accross pyinstaller from this post and it worked first time.

Installed it:

pip install pyinstaller

Compiled my program:

pyinstaller myprogram.py -i icon.ico

Worked first time! Hope that helps.

Community
  • 1
  • 1
Chud37
  • 4,907
  • 13
  • 64
  • 116