Source

Using py2exe the right way

Hi guys how are you? I hope all of you are fine. Recently I was working on a PyQt project. The project was youtube-dl-GUI. It is a GUI for the ever popular youtube-dl project.

After writing all the code I decided to make an exe in order to ease the deployment of my project on windows. For this purpose I decided to use py2exe which suited best to my needs. However it is necessary to know that py2exe is not the only Python to exe compiler out there. Some of the other popular Python exe makers out there are pyinstaller and cx_freeze.

These exe makers simply compile our script to bytecode and packages it with a Python execultable so that our program/script can work on those Windows' PC’s which don’t have Python installed.

However during the exe making process I faced a couple of problems and there was not a single blogpost anywhere dedicated to solving all of those problems. In this post I will try to list all of those problems and will also list the solutions which I used to solve those problems.

MSVCP90.dll missing

The first error which I came across was the MSVCP90.dll missing error. I searched on Google and came to know that I needed to install the Microsoft Visual C++ 2008 Redistributable Package in order to solve the problem. I came to know that it was already installed on my system but I still reinstalled it just to be sure. After reinstallation I tried to run py2exe again but the problem still persisted. After searching endlessly for an hour or so I came forward to a solution. The solution was to search and copy MSVCP90.dll from my system folder to Python’s DLL folder which in my case was C:\Python27\DLLs (it might be different on your system). I applied the solution and Voila it worked.

No module named sip

Another error I came across while compiling a PyQt4 script was the ImportError: No module named sip error. This one was pretty easy to solve. The solution was located on py2exe website. One solution was to use py2exe like this:

python setup.py py2exe --includes sip

The content of the setup.py were as follows:

from py2exe.build_exe import py2exe
from distutils.core import setup
setup( console=[{"script": "main.py"}] )

Another solution was to modify the setup.py file like this:

from distutils.core import setup
import py2exe
setup(windows=[{"script":"main.py"}], options={"py2exe":{"includes":["sip"]}})

And then run py2exe like this:

python setup.py py2exe

These solutions solved the problem for me.

No images displayed in PyQt

This solution was again hard to find but however I was able to find it. The solution was to package the PyQt image plugins with the application. I added this in my setup.py file:

DATA=[('imageformats',['C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qjpeg4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qgif4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qico4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qmng4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qsvg4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qtiff4.dll'
    ])]
setup( 
    #...
    #...
    data_files = DATA,
)

This solved the problem for me.

Making a single exe

Previously I use innosetup to make an installer but later I came across another method which allowed me to use py2exe to directly create a bundled exe which could be run by double clicking. In order to achieve this I modified my setup.py file like this:

from distutils.core import setup
import py2exe, sys, os
#...
#...
setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True,"includes":["sip"]}},
    #...
    #...
)

Please note that if you want to make an installer you need to use innosetup or an equivalent.

Finally

After making all those edits I ended up with something like this:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

DATA=[('imageformats',['C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qjpeg4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qgif4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qico4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qmng4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qsvg4.dll',
    'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qtiff4.dll'
    ])]

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True,"includes":["sip"]}},
    windows = [{'script': "main.py"}],
    zipfile = None,
    data_files = DATA,
)

I hope this post allowed you to solve some of your py2exe problems. If you need any further help then comment below, shoot me an email or tweet it directly to me.

Newsletter

×

If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

Abdelbasset Oueryemchi

I was looking for how to make it a simple exe file, Thanks :)

Yasoob
In reply to Abdelbasset Oueryemchi

I am glad that you liked it :)

Chuck Miller

Just started delving into py2exe. Thanks for posting these tips.

Jason Reed

For some reason I’m still getting the Missing Sip error even though I’ve called for it in my setup.py script. I’m also running it in the command line as “python setup.py py2exe” but when I run the exe I still get that error. However, running the command “python setup.py py2exe –includes sip compiles the project just fine. The only problem: I’m missing more modules and don’t know how to list multiple modules other than in the setup.py file. Please help?

johndlawlor
In reply to Jason Reed

You can expand whats in the includes section for what ever missing modules error messages you are getting

options={"py2exe":{"includes":["sip","sys","decimal"]}})

Rzezagar

Amazing! Those were exactly the problems I had with py2exe. Thank you, especially for the one with images and pyQt.

antoniods

Works solving error: ImportError: DLL load failed: PyQt4.QtGui.pyd. thx a lot

Josh Bradshaw

This was super helpful, thank you

ru4mj12 (@ru4mj12)

Using PyQt4 / Python 3.4, and wanting a single executable (not a folder of files), I think that limits me to using py2exe instead of some of the other newbie-friendly gui-based tools (cx_freeze , pyInstaller, etc).

Unfortunately, while my .py file runs fine when I execute it in tge IDLE GUI, it doesn’t run from the console due to ‘no module named xxxx’ errors.

I assume I have to get that to work first before running py2exe… but what makes it work from the IDLE gui and not via a command prompt?

felipe

hi there, i am trying to make a .exe file , i already had an partial success. i am using tkinter, matplotlib and numpy library´s. i tried a lot of variables ,but there is always an error. the most common is ‘‘matplot file data not found’’. have you ever had this problem? please, help me. felipe

Pierre Dimo

Excellent synthesis of regular outcoming questions about py2exe. However, there is one missing : how to automatically add the *.ui files (PyQt generated ) to the dist directory, without converting them to a python file ? Is there any solution for this ?

Sudeep

It says no module named py2exe_util please help

johndlawlor

I’ve an icon associated with my app, but when i convert it to an exe the icon is not present. Any information on how I might include it?

In the UI file the line is: icon.addPixmap(QtGui.QPixmap(_fromUtf8(“icons/favicon.ico”)), QtGui.QIcon.Normal, QtGui.QIcon.Off)

So I’ve added icons/favicon.ico to the dist directory, but no joy.

Thanks.

Yasoob
In reply to johndlawlor

Hey there! Thank you for replying. I am not so sure about what the problem might be because I have not worked with py2exe and windows recently. However, I do know that py2exe has a grudge with task-bar icons and even I had a hard time getting them to work properly.

vishal

Hi Yassob,

I faced another problem where, when i click the exe file, it run rapidly and shuts, does not hold the window, looks like I am missing something during creation, can you guide.

Tek Mailer

Vishal, try ending your code in a raw_input….

endRun = raw_input(r" “)
or put your run in a “while” loop that can only escape with a flag of some sort.

gilles

Hi Yassob, You’re first point caught my eye as I ran into that a few years back. I had found a relatively good answer at “http://stackoverflow.com/questions/12965238/wxpython-py2exe-app-not-bundling-properly"

In any case, adding ‘MSVCP90.dll’ to the excludes appears to have worked in my case.

PY2EXE (or CXFreeze) all seem to be hit and miss at times and gathering from all the forum chatter, needs a lot of tweaking at time. Other issues I ran into: Ex. the vlc binding might not py2exe properly work depending on the vlc.py and python versions you are trying, then furthermore it may or may not py2exe properly depending on the bundle option. Currently also having problems trying to py2exe an app that uses pysnmp (4.2.5 and/or 4.3) .

Anyways I hope the posting on “wxpython-py2exe-app-not-bundling-properly” may help explain a few things.

Kirk-In-Toronto

Please help…

C:\Python27\python.exe C:\Python27\projects\setup.py py2exe

Windows10 / Python27 / Tkinter – The following appear to be missing [‘PIL’]

… and thanks

Guray

searching for days, this page saved me in minutes! thanx!

Zach

Good lord, this is an excellent writeup. Better than the official documentation even. Thank you!

Ashish

Thanks … :)

Sergio Frayle Perez

Hi I have been using py2exe for some time and I too have had similar problems but I managed to solve them, but right now I am really stuck on one that I can’t fix. The thing is that I must use gnuradio.wxgui for the app I am currently developing, instead of PyQt which is what I have always used. For PyQt I include “PyQt4.QtGui” and “PyQt4.QtCore” in the setup.py because I always use these modules, without them a can’t build the exe. But now for the gnuradio.wxgui app I don’t know what happens that I manage to build the exe and when I run it, it shows up but it doesn’t respond, it just freezes, nothing happens and any click on the app causes it to close. I was wondering if there was any special module that must be included as in the PyQt case. If you happen to know what is causing this I would really appreciate your help.
Thank you so much Sergio Frayle

Katya Bostaph

Thank you so much! Creates a standalone executable file and no need for two folders with so many additional files.

used your tip on the following example:

#hello_w.py
f = open('helloworld.txt','w')
f.write('hello world')
f.close()

#setup.py
from distutils.core import setup
import py2exe
sys.argv.append('py2exe')
setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    windows = [{'script': "hello_w.py"}],
    zipfile = None
)

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊

OK