How to Include a Folder with Cx_Freeze

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?

Include entire folders in cx_Freeze with pygame

You just need to include the upper-level directory item in your options dictionary:

setup(name='Widgets Test',
version = '1.0',
description = 'Test of Text-input Widgets',
author = "Fred Nurks",
options = { "build_exe": {"packages":["pygame"], "include_files":["assets/", "music/"] } },
executables = executables
)

The above example will include the files assets/images/blah.png and music/sounds/sproiiiing.ogg along with their correct directories. Everything under that top-level folder is pulled into the lib/.

When you go to load these files, it's necessary to work out the exact path to the files. But the normal method to do this does not work with cxFreeze. Referencing the FAQ at https://cx-freeze.readthedocs.io/en/latest/faq.html ~

if getattr(sys, 'frozen', False):
EXE_LOCATION = os.path.dirname( sys.executable ) # frozen
else:
EXE_LOCATION = os.path.dirname( os.path.realpath( __file__ ) ) # unfrozen

Obviously you need the modules sys and os.path for this.

Then when loading a file, determine the full path with os.path.join:

my_image_filename = os.path.join( EXE_LOCATION, "assets", "images", "image.png" )
image = pygame.image.load( my_image_filename ).convert_alpha()

EDIT: If you're building under Windows, you will need to also include the Visual C runtime: https://cx-freeze.readthedocs.io/en/latest/faq.html#microsoft-visual-c-redistributable-package . Add include_msvcr to options.

options = { "build_exe": { "include_msvcr", "packages":["pygame"] #... 

Adding files and folders using cx_Freeze

The include_files option of the build_exe command should provide the solution you are looking for. According to the cx_Freeze documentation:

list containing files to be copied to the target directory; it is expected that this list will contain strings or 2-tuples for the source and destination; the source can be a file or a directory (in which case the tree is copied except for .svn and CVS directories); the target must not be an absolute path

Accordingly, try the following in your setup.py file:

from cx_Freeze import setup, Executable

build_exe_options = {"include_files": ["locales", "key.ico"],
...
}

setup(
...
options = {"build_exe": build_exe_options},
...
)

With this, cx_Freeze should make the expected copies, provided your setup.py file is located in src and you run the command

python setup.py build

from there as well.

Python cx_freeze Create dirs for included files in build

There are several ways you can go around solving the problem.

Method 1 - Using include_files

Rather than ask for each individual text file you could just put the file name in the setup script and leave out the individual text files. In your case it would be like this:

"include_files": ["databases"]

This would copy the entire databases folder with everything in it into you build folder.

Absolute file paths work as well.

If you are going to use the installer feature (bdist_msi) this is the method to use.

You can copy sub folders only using "include_files": ["databases/ACN"]

Method 2 - Manually

Ok it's rather un-pythonic but the one way to do it is to copy it manually into the build folder.

Method 3 - Using the os module

Much the same as method two it would copy the folder into your build folder but instead of coping it manually it would use Python. You also have the option of using additional Python features as well.

Hope I was helpful.

How to include files from a subdirectory in cxfreeze setup

If I understand correctly, you want to do the following.

BUILD_DIR/path_to_file/somefile.txt

to

DEST_DIR/somefile.txt

where dest dir also contains the {program}.exe

Try the following using the include_files like this:

include_files = 
[
('path_to_file/somefile.txt', 'somefile.txt'),
('path_to_file/otherfilename.txt, 'newfilename.txt)
]

The second line demonstrates renaming the file by changing the second name.

If I understand the description above correctly, this is specific to the example:

include_files=[
('daysgrounded/AUTHORS.txt','AUTHORS.txt'),
('daysgrounded/CHANGES.txt','CHANGES.txt'),
('daysgrounded/LICENSE.txt','LICENSE.txt'),
('daysgrounded/README.txt','README.txt'),
('daysgrounded/README.rst','README.rst'),
NAME],

How to put the .pyd and subfolders of a cx_Freeze executable in a single folder separately from the executable

I have managed to solve the issue.

The feature was planned for version 5.x, but due to internal changes, didn't make it.

It is present in as-of-now unreleased versions, though. I solved it by downloading the source for cx_Freeze from its repository and following instructions to compile and install it. Now there's just 3 DLLs in the executable's folder and the rest of the files are in a single lib folder, much cleaner.

Image directories and cx_Freeze

You can include a relative directory in your "include_files" directive. Something like this:

include_files = [("game_assets", "assets")]

That will copy all of the files in the directory "game_assets" to a target directory "assets". You can use whatever you wish for the names, of course!

cx_Freeze common lib folder between executables

Yes it is possible. The trick is to use a single setup.py where the multiple scripts are added to the executables list.

Take for example the following pair of console-based scripts which both use numpy:

main1.py:

import numpy

print('Program 1, numpy version %s' % numpy.__version__)
input('Press ENTER to quit')

main2.py:

import numpy

print('Program 2, numpy version %s' % numpy.__version__)
input('Press ENTER to quit')

You can freeze this scripts at once with cx_Freeze using the following setup.py:

from cx_Freeze import setup, Executable

base = None

executables = [Executable('main1.py', base=base),
Executable('main2.py', base=base)]

additional_mods = ["numpy.core._methods", "numpy.lib.format"]

build_exe_options = {"includes": additional_mods}

setup(name='MYSCRIPTS',
version='0.1',
options={"build_exe": build_exe_options},
description='MYSCRIPTS',
executables=executables)

You get then two executables main1.exe and main2.exe sharing the same lib folder containing numpy.



Related Topics



Leave a reply



Submit