Python cx freeze: Difference between revisions
No edit summary |
|||
Line 4: | Line 4: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| Official Docs || https://cx-freeze.readthedocs.io/en/latest/ | | Official Docs || https://cx-freeze.readthedocs.io/en/latest/ | ||
|} | |} | ||
Line 47: | Line 47: | ||
== build_exe options == | == build_exe options == | ||
<blockquote> | <blockquote> | ||
cx_freeze configured using the <code>Executable</code> class, and the options for <code>build_exe</code>. | cx_freeze configured using the <code>Executable</code> class, and the options for <code>build_exe</code>.<br> | ||
Official Documentation: http://cx-freeze.readthedocs.io/en/latest/distutils.html#build-exe | Official Documentation: http://cx-freeze.readthedocs.io/en/latest/distutils.html#build-exe | ||
Common Options: | |||
<source lang="python"> | <source lang="python"> | ||
setup( | setup( | ||
options={ | options={ | ||
'build_exe': { | 'build_exe': { | ||
# ======= | |||
# general | # general | ||
# ======= | |||
' | |||
' | # change build-directory. defaults to 'build', | ||
' | 'build_exe': 'alt/build', | ||
# compress build into zipfile (-c argument), | |||
'compressed': True, | |||
# bundle windows deps (internal only, violates windows TOS) | |||
'include_msvcr': True, | |||
# use relative-paths in stacktraces (instead of absolute paths from builder) | |||
'replace_paths': [('*', '')], | |||
# ======== | |||
# packages | # packages | ||
# ======== | |||
# paths to search for modules (defaults to sys.path value) (!WARNING!: must include python!) | |||
'path': ['/src', '/other/src'], | |||
# list of packages to include (incl. submodules) | |||
'packages': ['foo'], | |||
# list of specific modules to include (not incl. submodules) | |||
'includes': ['foo', 'foo.lib', 'foo.lib.abc'], | |||
# list of specific modules to exclude (not incl. submodules) | |||
'excludes': ['bar.core', 'bar.core.constants', ..], | |||
# ========= | |||
# datafiles | # datafiles | ||
'include_files': [ ('/src/file', '/dst/file'), '/path/file'] | # ========= | ||
# datafiles to include. either (src,dst), or path | |||
'include_files': [ ('/src/file', '/dst/file'), '/path/file'] | |||
} | } | ||
} | } | ||
Line 92: | Line 114: | ||
} | } | ||
</source> | </source> | ||
</blockquote><!-- bdist_* options --> | </blockquote><!-- bdist_* options --> | ||
Line 103: | Line 124: | ||
targetName='execute-this.exe', # name of executable | targetName='execute-this.exe', # name of executable | ||
icon='path/to/icon.ico', # .ico file to use as icon (must be .ico) | icon='path/to/icon.ico', # .ico file to use as icon (must be .ico) | ||
# add to start-menu | # add to start-menu | ||
shortcutName=' | shortcutName='my-executable', | ||
shortcutDir='ProgramFilesFolder', # can be any system-folder-property (see link below) | shortcutDir='ProgramFilesFolder', # can be any system-folder-property (see link below) | ||
# to add to desktop, use 'DesktopFolder' | # to add to desktop, use 'DesktopFolder' | ||
Line 143: | Line 164: | ||
By default <code>Executable('yourscript.py', base=None)</code>: | By default <code>Executable('yourscript.py', base=None)</code>: | ||
* when run from cmd, the executable runs inside that cmd window. | * when run from cmd, the executable runs inside that cmd window. | ||
* When double clicked, a visible CMD window is created to run the .exe. | * When double clicked, a visible CMD window is created to run the .exe. | ||
Alternatively, you may set <code>Executable('yourscript.py', base='Win32GUI')</code>. | Alternatively, you may set <code>Executable('yourscript.py', base='Win32GUI')</code>. | ||
* when run from cmd, the executable runs in a new, hidden cmd window. Console output is not forwarded. | * when run from cmd, the executable runs in a new, hidden cmd window. Console output is not forwarded. | ||
* when double clicked, the executable opens without a cmd window being created. | * when double clicked, the executable opens without a cmd window being created. | ||
Line 162: | Line 183: | ||
See [[cx_freeze workarounds]]. | See [[cx_freeze workarounds]]. | ||
</blockquote><!-- Package notes --> |
Revision as of 16:12, 2 July 2022
cx_freeze lets you freeze your python application into a standalone executable.
Official Docs | https://cx-freeze.readthedocs.io/en/latest/ |
Install
Usage
python setup.py build_exe # build executable python setup.py bdist_msi # build msi installer python setup.py bdist_dmg # build dmg installer python setup.py clean --all # note the required (--all)
Project Setup
Example
Instructions for setup.py are written in your
setup.py
.
setup.pyimport setuptools from cx_Freeze import setup, Executable setup( executables=[Executable('project/exename.py'), Executable('project/exename2.py', icon='file.ico')], options={'build_exe':{'packages': ['pkg_resources._vendor'], 'includes': []}}, )build_exe options
cx_freeze configured using the
Executable
class, and the options forbuild_exe
.
Official Documentation: http://cx-freeze.readthedocs.io/en/latest/distutils.html#build-exeCommon Options:
setup( options={ 'build_exe': { # ======= # general # ======= # change build-directory. defaults to 'build', 'build_exe': 'alt/build', # compress build into zipfile (-c argument), 'compressed': True, # bundle windows deps (internal only, violates windows TOS) 'include_msvcr': True, # use relative-paths in stacktraces (instead of absolute paths from builder) 'replace_paths': [('*', '')], # ======== # packages # ======== # paths to search for modules (defaults to sys.path value) (!WARNING!: must include python!) 'path': ['/src', '/other/src'], # list of packages to include (incl. submodules) 'packages': ['foo'], # list of specific modules to include (not incl. submodules) 'includes': ['foo', 'foo.lib', 'foo.lib.abc'], # list of specific modules to exclude (not incl. submodules) 'excludes': ['bar.core', 'bar.core.constants', ..], # ========= # datafiles # ========= # datafiles to include. either (src,dst), or path 'include_files': [ ('/src/file', '/dst/file'), '/path/file'] } } )bdist_* options
cx_Freeze can also create platform-specific installers (windows(msi), osx(dmg), ...). These have their own sets of options.
setup( options={ "bdist_msi": { "add_to_path": True, # add to %PATH% variable "initial_target_dir": r'[ProgramFilesFolder]\MyCompany', # control install directory } }cx_Freeze.Executable()
executable name:
cx_Freeze.Executable( targetName='execute-this.exe', # name of executable icon='path/to/icon.ico', # .ico file to use as icon (must be .ico) # add to start-menu shortcutName='my-executable', shortcutDir='ProgramFilesFolder', # can be any system-folder-property (see link below) # to add to desktop, use 'DesktopFolder' )
bdist_msi desktop shortcut https://stackoverflow.com/questions/15734703/use-cx-freeze-to-create-an-msi-that-adds-a-shortcut-to-the-desktop bdist_msi system-folder-properties https://docs.microsoft.com/en-us/windows/desktop/Msi/property-reference#system-folder-properties creating .ico files with imagemagick imagemagick finding datafiles
if getattr(sys, 'frozen', False): print('get file from frozen location') else: print('get file from normal location')
Platform Notes
Windows
Console VS GUI exe
On windows you have two choices for how you want your program to run:
By defaultExecutable('yourscript.py', base=None)
:
- when run from cmd, the executable runs inside that cmd window.
- When double clicked, a visible CMD window is created to run the .exe.
Alternatively, you may set
Executable('yourscript.py', base='Win32GUI')
.
- when run from cmd, the executable runs in a new, hidden cmd window. Console output is not forwarded.
- when double clicked, the executable opens without a cmd window being created.
My solution for this is to create amyscript
andmyscript-cli
. It's not perfect, but it is a serviceable solution.
Package notes
Some packages are more problematic than others. Here are some packages, their issue and solutions.