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

发布于 2024-08-26 21:51:15 字数 1028 浏览 10 评论 0原文

我在 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

这是我的片段:

设置

( name='看门人',
  版本='1.0',
  描述='清洁工',
  作者='约翰·多伊',
  author_email='[电子邮件受保护]',
  url='http://www.this-page-intentionally-left-blank.org/',
  数据文件= 
      [ ('helpers\uncompress', ['helpers\uncompress\unzip.exe']),
        ('helpers\uncompress', ['helpers\uncompress\unRAR.exe']),
        ('', ['README.txt'])
      ],
  可执行文件=
      [
      可执行文件\
          (
          '看门人.py',#initScript
          )
      ]
)

我似乎无法让它工作。我需要 MANIFEST.in 文件吗?

I'm using Python 2.6 and cx_Freeze 4.1.2 on a Windows system. I've created the setup.py to build my executable and everything works fine.

When cx_Freeze runs, it moves everything to the build directory. I have some other files that I would like included in my build directory. How can I do this? Here's my structure:

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

Here's my snippet:

setup

( name='Janitor',
  version='1.0',
  description='Janitor',
  author='John Doe',
  author_email='[email protected]',
  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
          )
      ]
)

I can't seem to get this to work. Do I need a MANIFEST.in file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三岁铭 2024-09-02 21:51:15

想通了。

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 = '[email protected]',
    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)

Figured it out.

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 = '[email protected]',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable('janitor.py')]
)

Note:

  • include_files must contain "only" relative paths to the setup.py script else the build will fail.
  • include_files can be a list of string i.e a bunch of files with their relative paths
    or
  • include_files can be a list of tuples in which the first half of the tuple is the file name with the absolute path and the second half is the destination filename with the absolute path.

(When the lack of the documentation arises, consult Kermit the Frog)

猫性小仙女 2024-09-02 21:51:15

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

所有选项的缺少文档位于:cx_Freeze(互联网档案馆)

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

替代方案: 包装 |老鼠VS。 Python

There's a more complex example at: cx_freeze - wxPyWiki

The lacking documentation of all the options is at: cx_Freeze (Internet Archive)

With cx_Freeze, I still get a build output of 11 files in a single folder, though, unlike with Py2Exe.

Alternatives: Packaging | The Mouse Vs. Python

风苍溪 2024-09-02 21:51:15

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

请参阅 cx-freeze:使用数据文件

In order to find your attached files (include_files = [-> your attached files <-]) you should insert the following function in your setup.py code:

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

噩梦成真你也成魔 2024-09-02 21:51:15

您还可以创建单独的脚本,在构建后复制文件。我用它在 Windows 上重建应用程序(您应该安装“GNU utility for win32”以使“cp”工作)。

构建.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

Also you can create separate script that will copy files after the build. It's what I use to rebuild the app on windows (you should have "GNU utilities for win32" installed to make "cp" works).

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文