【问题标题】:How can I bundle other files when using cx_freeze?使用 cx_freeze 时如何捆绑其他文件?
【发布时间】:2011-02-02 23:27:06
【问题描述】:

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

当 cx_Freeze 运行时,它会将所有内容移动到 build 目录。我有一些其他文件,我想将它们包含在我的build 目录中。我怎样才能做到这一点?这是我的结构:

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

这是我的 sn-p:

设置

( 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 文件吗?

【问题讨论】:

    标签: python distutils cx-freeze


    【解决方案1】:

    想通了。

    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 可以是一个元组列表,其中元组的前半部分是带有绝对路径的文件名,后半部分是带有绝对路径的目标文件名。

    (当缺少文档时,请咨询 Kermit the Frog)

    【讨论】:

    • 谢谢克雷格。我尝试加入你所在的邮件列表,但我不被允许。你能帮帮我吗?是否可以指定目标目录? include_files 选项只是在 builds 目录中创建了相同的目录结构并将包含的文件转储到那里。谢谢。
    • 也解决了这个问题。我通过源。遗憾的是,没有足够好的文档记录。我很乐意帮助为此编写一些文档。
    • @MridangAgarwalla,非常感谢! =D
    • include_files 列表可以同时包含具有相对路径的字符串和具有绝对路径的元组。我有一个带有我需要的绝对路径的奇怪文件,而我的所有其余文件都在项目文件夹中。没问题。
    • includes 是干什么用的?
    【解决方案2】:

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

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

    cx_Freeze 不同,与Py2Exe 不同,我仍然在单个文件夹中获得11 个文件的构建输出。

    替代方案:Packaging | The Mouse Vs. Python

    【讨论】:

    • cx_freeze 永远不会支持单文件 .exe 文件,因为开发人员认为用来做这种事情的“黑客”并不干净。如果你想要一个文件,那么你将不得不坚持使用 Py2exe,这里是 feature request for Python3 support
    • 您的缺少文档的链接现已失效。如果你仍然使用这个网站,我希望能提供一个关于选项的文档的指针,因为我对它们不够了解
    • J.F. Sebastian 将文档链接修复为存档副本。 The current page 的信息似乎较少。
    【解决方案3】:

    您还可以创建单独的脚本,在构建后复制文件。这是我用来在 Windows 上重建应用程序的工具(您应该安装“用于 win32 的 GNU 实用程序”以使“cp”正常工作)。

    build.bat:

    cd .
    del build\*.* /Q
    python setup.py build
    cp -r icons build/exe.win32-2.7/
    cp -r interfaces build/exe.win32-2.7/
    cp -r licenses build/exe.win32-2.7/
    cp -r locale build/exe.win32-2.7/
    pause
    

    【讨论】:

      【解决方案4】:

      为了找到您的附件 (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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-11
        • 1970-01-01
        • 1970-01-01
        • 2014-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多