help, installation, packaging, python, tutorial

Using py2exe the right way

Edit: I am developing a new blog in Python using Pelican. I have posted this post on that blog as well. Make sure that you visit that blog and offer any advice if you have.

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 besst 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.

About these ads
Standard

6 thoughts on “Using py2exe the right way

  1. Pingback: Python-digest #17. Новости, интересные проекты, статьи и интервью [2 марта 2014 — 9 марта 2014] | Вести3.ру — Информационный журнал

  2. Pingback: Python-digest #17. Новости, интересные проекты, статьи и интервью [2 марта 2014 — 9 марта 2014] » CreativLabs

  3. Jason Reed says:

    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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s