使用cx_freeze时如何捆绑其他文件?

2024-09-28 20:59:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows系统上使用Python 2.6和cx_Freeze 4.1.2。我创建了setup.py来构建我的可执行文件,一切正常。

当cxúu Freeze运行时,它会将所有内容移动到build目录。我还有一些其他文件想包含在我的build目录中。我该怎么做?这是我的结构:

src\
    setup.py
    janitor.py
    README.txt
    CHNAGELOG.txt
    helpers\
        uncompress\
            unRAR.exe
            unzip.exe

这是我的片段:

setup

( name='Janitor',
  version='1.0',
  description='Janitor',
  author='John Doe',
  author_email='john.doe@gmail.com',
  url='http://www.this-page-intentionally-left-blank.org/',
  data_files = 
      [ ('helpers\uncompress', ['helpers\uncompress\unzip.exe']),
        ('helpers\uncompress', ['helpers\uncompress\unRAR.exe']),
        ('', ['README.txt'])
      ],
  executables =
      [
      Executable\
          (
          'janitor.py', #initScript
          )
      ]
)

我好像没法让它工作。我需要一个MANIFEST.in文件吗?


Tags: 文件pybuild目录txtsetupexereadme
3条回答

有一个更复杂的例子在:cx_freeze - wxPyWiki

缺少所有选项的文档位于:cx_Freeze (Internet Archive)

但是,使用cx_Freeze时,与使用Py2Exe不同,我仍然可以在单个文件夹中获得11个文件的生成输出。

备选方案:Packaging | The Mouse Vs. Python

要查找附加文件(include_files = [-> your attached files <-]),应在setup.py代码中插入以下函数:

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

See cx-freeze: using data files

明白了。

from cx_Freeze import setup,Executable

includefiles = ['README.txt', 'CHANGELOG.txt', 'helpers\uncompress\unRAR.exe', , 'helpers\uncompress\unzip.exe']
includes = []
excludes = ['Tkinter']
packages = ['do','khh']

setup(
    name = 'myapp',
    version = '0.1',
    description = 'A general enhancement utility',
    author = 'lenin',
    author_email = 'le...@null.com',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable('janitor.py')]
)

注:

  • include_files必须包含指向setup.py脚本的“唯一”相对路径,否则生成将失败。
  • include_files可以是一个字符串列表,即具有相对路径的一组文件
  • include_files可以是元组列表,其中元组的前半部分是具有绝对路径的文件名,后半部分是具有绝对路径的目标文件名。

(当缺少文件时,请咨询青蛙克米特)

相关问题 更多 >