Search code examples
pythoncx-freeze

cx_freeze & bundling files


At present I am using pyinstaller for bundling my python application. I am equally migrating to pyGObject (due to pygtk being depreciated).

Now pyinstaller does not support pyGObject and I have as of yet not figured out the required hooks... One of the other downsides of pyinstaller is how it bundles into a single executable - it causes the company installed virus scanner to check quite intensively every time the exe is run ==> quite slow startup.

Looking into using cx_freeze due to the pyGObject & py3 support I note it does not have a single-executable option. That in itself isn't an issue if the working directory can be cleaned up, be it via the pyd/dll being bundled into a second zip or into a subdirectory.

Searching around (stackoverflow and other sites), it is illuded to that it can be done, but I am not getting the expected results. Any idea#s?

setup.py is based around this one: http://wiki.wxpython.org/cx_freeze


Solution

  • ok solved:

    1) setup.py

    import sys
    from cx_Freeze import setup, Executable
    EXE1 = Executable(
        # what to build
        script = "foo.py",
        initScript = None,
        base = 'Win32GUI',
        targetDir = "dist",
        targetName = "foo.exe",
        compress = True,
        copyDependentFiles = True,
        appendScriptToExe = True,
        appendScriptToLibrary = False,
        icon = 'foo.ico'
        )
    
    setup(
        version = "9999",
        description = "...",
        author = "...",
        name = "...",
    
        options = {"build_exe": {"includes": includes,
                                 "excludes": excludes,
                                 "packages": packages,
                                 "path": sys.path,
                                 "append_script_to_exe":False,
                                 "build_exe":"dist/bin",
                                 "compressed":True,
                                 "copy_dependent_files":True,
                                 "create_shared_zip":True,
                                 "include_in_shared_zip":True,
                                 "optimize":2,
                                 }
                   },
    
        executables = [EXE1]
        )
    

    2) foo.py header:

    import os
    import sys
    
    if getattr(sys,'frozen',False):
        # if trap for frozen script wrapping
        sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin'))
        sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin\\library.zip'))
        os.environ['TCL_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tcl')
        os.environ['TK_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tk')
        os.environ['MATPLOTLIBDATA'] = os.path.join(os.path.dirname(sys.executable),'bin\\mpl-data')