How to build a python package into an exe

Go To StackoverFlow.com

1

I have written my program in python. It is written across seven files and in three of the files I import a custom package. The thing is I cant build my program into an exe. I have tried pyinstaller 1.5.1 and py2exe. I have followed every tutorial I could find but with no success. Every time I have tried when I go to run the exe created I get an error message saying it cannot find my custom package. I think I have just not been able to get the complete package to be built into the exe. Please help.

from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')

mfcfiles = [os.path.join(mfcdir, i) for i in ["mfc90.dll", "mfc90u.dll", "mfcm90.dll", "mfcm90u.dll", "Microsoft.VC90.MFC.manifest"]]

data_files = [("Microsoft.VC90.MFC", mfcfiles),]

setup(
    data_files = data_files,
    options = {'py2exe': {'optimize': 2}},
    windows = [{'script': "LoadFilesGUI.py"}],
    zipfile = "shared.lib",
)

another:

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

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'optimize': 2}},
    windows = [{'script': "LoadFilesGUI.py"}],
    zipfile = None,
    console=['LoadFilesGUI.py'],
)
2012-04-05 16:58
by Misterman1982
You can do it. Try again. When you do, let us know more details about the file structure and packager config you tried - j13r 2012-04-05 17:04
Yes it should be able to work. I agree you need to show us your setup.py and what your project structure is lik - jdi 2012-04-05 17:13
what types of files? what types of custom packages? what is the error from pyinstaller and py2exe? Question leaving much more room for other questions - Jack_of_All_Trades 2012-04-05 17:34
My files are all just python files. Of these three are just for gui's. The error says "ImportEttor: No module named MyPackage". MyPackage is the package of files I am trying to call - Misterman1982 2012-04-05 20:13
@Misterman1982: I have answered to the best of my abilities with the current amount of info. This project structure could also be useful info to add to your question - jdi 2012-04-05 20:17
why people still try to use py2exe? looks like it is abandoned, last update was in 200 - tovmeod 2012-04-06 06:40
@tovmeod: Because it still works. Are you basing that date off the last zip on sourceforge? If you look at the actual repository you will see it was committed to within the last 2 months: http://py2exe.svn.sourceforge.net/viewvc/py2exe - jdi 2012-04-06 16:25
last release is dated 3 years ago on the link you provided me, how do I update to trunk - tovmeod 2012-04-07 21:16
@tovmeod: svn co https://py2exe.svn.sourceforge.net/svnroot/py2exe/trunk/py2exe py2exe
I think you are reading the info on that link wrong. It says under the trunk that recent updates have been made 2 months ago - jdi 2012-04-10 07:59
I have py2exe installed under site-packages, how do I update to trunk - tovmeod 2012-04-16 14:42


2

By "custom package", I am assuming you mean your custom python modules that make up your application. I might suggest trying to use the "includes" option of the setup() call to manually specify the python modules that should be included, in case they are not discovered automatically:

setup( 
    windows = [{'script': "LoadFilesGUI.py"}], 
    data_files = data_files, 
    zipfile = None, 
    options = {
        'py2exe': {
            'optimize': 2, 
            'bundle_files': 1,
            'includes' : [],
        }
    }, 
) 

For instance, in my project which uses PyQt4, my includes looks like this:

options={ 
    "py2exe": { 
        "includes" : ['sip','PyQt4.QtCore','PyQt4.QtGui',
                        'PyQt4.QtNetwork','PyQt4.QtWebKit'] 
    } 
}

The rest of your setup.py script seems to be pretty standard. Make sure that your included modules are part of your PYTHONPATH, either being relative to your project, or explicitly added to the path.

If, as you have stated in your comments, MyPackage is a package within your project, then you would want: 'includes' : ['MyPackage']

If this still gives you errors, then the problem could be with the structure of your project and package, or again, with your PYTHONPATH

2012-04-05 20:13
by jdi


0

cx_freeze is the answer.

From my experience is the most reliable option. Sometimes it misses some dependency, but you can easily add that manually once you discover what's missing.

2012-04-05 18:30
by Gabriel
Its not really the answer in this case. Its just you suggesting he use another package. I have successfully used py2exe on windows for a PyQt4 based python project. Surely there is something the OP is doing wrong - jdi 2012-04-05 18:38
Whatever you say, but cxFreeze works usually OOB from binary packages, is cross-platform and available from Python 2.3 up to 3.2, while py2exe, as far as i know, works only for Python 2.6. Moreover, customising a distutils script gives you a lot of flexibility (see http://cx-freeze.sourceforge.net/cxFreeze.html).

Free to keep trying with py2exe, but if there's a better tool why not to use it - Gabriel 2012-04-05 18:54

PS: that's a short tutorial: http://www.blog.pythonlibrary.org/2010/08/12/a-cx_freeze-tutorial-build-a-binary-series - Gabriel 2012-04-05 19:00
You seem to be making strange and uninformed claims. py2exe is not bound to python2.6, and it is a distutils extension just the same. My point in this comment is that your answer does not help the OP simply by saying to scrap his current attempts and try another tool. He could just as easily have trouble with cx_freeze. The proper answer is the one that helps the OP solve his stated problem. Unfortunately, he has not provided anyone enough information to do so - jdi 2012-04-05 19:17
Sorry I forgot to include in the setup code (How on earth did I do that?). Will include it now. From what I have read online py2exe is best with 2.6 and 2.7 but can work with up to 3 - Misterman1982 2012-04-05 19:47
@Misterman1982: PLEASE move this important information to your question (edit your question - jdi 2012-04-05 19:55
Ads