cx_Freeze可执行文件之间的公用lib文件夹

时间:2019-05-03 09:25:18

标签: python cx-freeze

我正在将一组python脚本编译为可执行文件。为此,我正在使用cx_Freeze。

一个相当普遍的问题是lib文件夹变得非常大。我已尽可能地排除模块以减小其大小,但它仍然相当大。

由于我正在编译多个可执行文件,是否可以有一个共享的lib共享文件夹来引用它们以减小磁盘大小?

示例setup.py如下:

import sys, os
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

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

additional_mods = ["numpy.core._methods", "numpy.lib.format"]
exclude_mods = ["babel", "scipy", "PyQt5", "tornado", "zmq", "sphinx", "sphinx_rtd_theme", "psutil", "notebook", "nbconvert", "lxml", "cryptography", "bottleneck", "matplotlib"]

build_exe_options = {"excludes": exclude_mods, "includes": additional_mods, "optimize": 1}

os.environ['TCL_LIBRARY'] = r'C:\ProgramData\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\ProgramData\Anaconda3\tcl\tk8.6'

setup(name='MYSCRIPT',
      version='0.1',
      includes = ['os'],
      options = {"build_exe": build_exe_options},
      description='MYSCRIPT',
      executables=executables
      )

1 个答案:

答案 0 :(得分:1)

是的,有可能。诀窍是使用单个setup.py,其中将多个脚本添加到executables列表中。

例如,下面两个都使用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')

您可以使用以下setup.py使用cx_Freeze立即冻结此脚本:

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)

然后您将获得两个可执行文件main1.exemain2.exe,它们共享包含numpy的同一lib文件夹。