如何在cxfreeze设置中包含子目录中的文件

1 投票
1 回答
1517 浏览
提问于 2025-04-18 05:48

我创建了一个叫做 cxfreeze_setup.py 的文件,并运行了命令 python cxfreeze_setup.py build_exe

这个 cxfreeze_setup.py 文件里包含了以下内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Python 3 compatibility
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import sys
from cx_Freeze import setup, Executable

from daysgrounded.globalcfg import NAME, VERSION, DATA_FILES
from daysgrounded import (DESC, LICENSE, URL, KEYWORDS, CLASSIFIERS)

AUTHOR = 'Joao Matos'
SCRIPT = NAME + '/__main__.py'
TARGET_NAME = NAME + '.exe'

base = None
# GUI applications require a different base on Windows
if sys.platform == 'win32':
    base = 'Win32GUI'

build_exe_options = dict(compressed=True,
                         include_files=['AUTHORS.txt',
                                        'CHANGES.txt',
                                        'LICENSE.txt',
                                        'README.txt',
                                        'README.rst',
                                        NAME],
                        )

setup(name=NAME,
      version=VERSION,
      description=DESC,
      long_description=open('README.txt').read(),
      #long_description=(read('README.txt') + '\n\n' +
      #                  read('CHANGES.txt') + '\n\n' +
      #                  read('AUTHORS.txt')),
      license=LICENSE,
      url=URL,
      author=AUTHOR,
      author_email='jcrmatos@gmail.com',

      keywords=KEYWORDS,
      classifiers=CLASSIFIERS,

      executables=[Executable(script=SCRIPT,
                              base=base,
                              compress=True,
                              targetName=TARGET_NAME,
                             )],

      options=dict(build_exe=build_exe_options),
     )

构建是成功的,但我想把同一目录下 NAME 子目录里的 *.txt 文件也包含进去,然而 include_files 只让我包含子目录,而不能把文件移动过来。

我想要的最终结果和用“正常”的构建命令一样,比如 python setup.py sdist bdist_egg bdist_wininst bdist_wheel 这样做是通过 setup.py 的选项来实现的。

include_package_data=True
package_data=dict(daysgrounded=['usage.txt', 'LICENSE.txt', 'banner.txt'])

还有 MANIFEST.in 文件的内容:

include daysgrounded\banner.txt
include daysgrounded\LICENSE.txt
include daysgrounded\usage.txt

谢谢,

JM

1 个回答

1

如果我理解得没错,你想做的是把这个文件:

BUILD_DIR/path_to_file/somefile.txt

移动到这个地方:

DEST_DIR/somefile.txt

而且目标文件夹里还要有这个{program}.exe文件。

你可以试试下面这个方法,使用include_files,像这样:

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

第二行展示了如何通过改变第二个名字来重命名文件。

如果我理解上面的描述没错,这个是针对特定例子的:

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],

撰写回答