Include only needed parts of Python to final build exe

1.2k views Asked by At

I was wondering if there is a way to reduce file size when creating a Python exe using cx_freeze or something else.

Excluding unnecessary modules is suggested before, but rather than that, I am trying to find if it is possible to actually exclude parts of the Python itself.

When building the exe, I observed many stuff I didn't need was included in the build file (Almost all of Lib directory, many stuff from site-packages etc). I ask if we can exclude these ? Or better, can I only include only the things I need. (need to find what I need first)

And for even more, is it possible to exclude unused built-in features of Python itself ? even if it means rebuilding Python from source ?

Thanks for your time.

Edit: Maybe they are already being excluded, I may have misinterpreted the situation. But I had to manually exclude Tkinter. Which made my question if more could be removed.

Edit2: Here is the script file:

from cx_Freeze import setup, Executable
import os

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

base = None

executables = [Executable("serdar.py", base=base)]

includefiles = ['zombie.png', 'human.jpeg']
packages = []
excludes = ["tkinter"]

options = {
    'build_exe': {
        'packages':packages,
        'excludes':excludes,
        'include_files':includefiles,
        "optimize": 2
    },

}

setup(
    name = "serdar",
    options = options,
    version = "0.01",
    description = 'None',
    executables = executables
)
1

There are 1 answers

6
karol On

In setup.py try include only packages which you want as in example:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

from documentation: "A common problem is that cx_Freeze hasn’t automatically detected that a file needs to be copied. Modules that your code imports are detected, but if they’re dynamically loaded - e.g. by a plugin system - you have to tell cx_Freeze about them. "