How to Bundle Other Files When Using Cx_Freeze

How can I bundle other files when using cx_freeze?

Figured it out.

from cx_Freeze import setup,Executable

includefiles = ['README.txt', 'CHANGELOG.txt', 'helpers\uncompress\unRAR.exe', , 'helpers\uncompress\unzip.exe']
includes = []
excludes = ['Tkinter']
packages = ['do','khh']

setup(
name = 'myapp',
version = '0.1',
description = 'A general enhancement utility',
author = 'lenin',
author_email = 'le...@null.com',
options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [Executable('janitor.py')]
)

Note:

  • include_files must contain "only" relative paths to the setup.py script else the build will fail.
  • include_files can be a list of string i.e a bunch of files with their relative paths

    or
  • include_files can be a list of tuples in which the first half of the tuple is the file name with the absolute path and the second half is the destination filename with the absolute path.

(When the lack of the documentation arises, consult Kermit the Frog)

cx_freeze & bundling files

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

How can I include a folder with cx_freeze?

You have to set up an include files argument for the building options. You can do this in different ways, but I will show a part of my configuration. The thing I describe here is for one specific file and one specific destination. I think you can also set a path like this, but I don't have tested this yet.

Edit: Tested this, so choose the right approach for your project.

buildOptions = dict(include_files = [(absolute_path_to_your_file,'final_filename')]) #single file, absolute path.

buildOptions = dict(include_files = ['your_folder/']) #folder,relative path. Use tuple like in the single file to set a absolute path.

setup(
name = "appname",
version = "1.0",
description = "description",
author = "your name",
options = dict(build_exe = buildOptions),
executables = executables)

Take also a look at this topic. It adressed propably the same question: How can i bundle other files when using cx_freeze?

cx_Freeze not including specified files

You need to move your images to the build directory so the executable can find them. I doubt cx_Freeze will package the images into the executable.

Python cx_Freeze for two or more python files (modules)

If your hello.pyfile import those files - hello1.py and hello2.py, then this line:

executables = [Executable("hello.py")])

is quite enough.

But if any of those files are separate script file, then you should do like this:

from cx_Freeze import setup, Executable

setup(
name = "hello",
version = "0.1",
description = "the typical 'Hello, world!' script",
executables = [Executable("hello.py"), Executable("hello1.py"), Executable("hello2.py")]
)

It will create 3 .exe files, for each one of your scripts.

How to make cx_Freeze compile submodules into shared object (.so) files?

It isn't possible.

I emailed the creator of cx_Freeze (Anthony Tuininga) and this was his response:

cx_Freeze doesn't have that capability. It puts all of its compiled Python code (.pyc files) into a zip file. Technically, there is a way to do so, but cx_Freeze doesn't do so. Since it involves a C compiler its more complex and prone to error.



Related Topics



Leave a reply



Submit