Search code examples
pythonpy2exepyinstaller

How to build a python package into an exe


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'],
)

Solution

  • 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