cx_Freeze - Preventing including unneeded packages

后端 未结 3 1377
心在旅途
心在旅途 2021-02-04 04:49

I have coded a tiny python program using PyQt4. Now, I want to use cx_Freeze to create a standalone application. Everything works fine - cx_Freeze includes automatically all nec

3条回答
  •  礼貌的吻别
    2021-02-04 05:33

    This is how I optimized my executable to the minimum file size

    from cx_Freeze import setup, Executable
    import subprocess
    import sys
    
    
    NAME = 'EXE NAME'
    VERSION = '1.0'
    PACKAGES = ['pygame', ('import_name', 'package_name')]
    # if names are same just have a string not a tuple
    installed_packages = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']).decode('utf-8')
    installed_packages = installed_packages.split('\r\n')
    EXCLUDES = {pkg.split('==')[0] for pkg in installed_packages if pkg != ''}
    EXCLUDES.add('tkinter')
    for pkg in PACKAGES:
        if type(pkg) == str: EXCLUDES.remove(pkg)
        else: EXCLUDES.remove(pkg[1])
    
    
    executables = [Executable('main.py', base='Win32GUI', icon='Resources/Jungle Climb Icon.ico', targetName=NAME)]
    
    setup(
        name=NAME,
        version=VERSION,
        description=f'{NAME} Copyright 2019 AUTHOR',
        options={'build_exe': {'packages': [pkg for pkg in PACKAGES if type(pkg) == str else pkg[0]],
                               'include_files': ['FOLDER'],
                               'excludes': EXCLUDES,
                               'optimize': 2}},
        executables=executables)
    

提交回复
热议问题