让 py2exe 包含我的数据文件(如 include_package_data)

发布于 2024-08-30 18:56:55 字数 313 浏览 19 评论 0原文

我有一个 Python 应用程序,它的一些子包中包含非 Python 数据文件。我一直在 setup.py 中使用 include_package_data 选项来在制作发行版时自动包含所有这些文件。效果很好。

现在我开始使用 py2exe。我希望它看到我有 include_package_data=True 并包含所有文件。但事实并非如此。它只将我的 Python 文件放入 library.zip 中,因此我的应用程序无法运行。

如何使 py2exe 包含我的数据文件?

I have a Python app which includes non-Python data files in some of its subpackages. I've been using the include_package_data option in my setup.py to include all these files automatically when making distributions. It works well.

Now I'm starting to use py2exe. I expected it to see that I have include_package_data=True and to include all the files. But it doesn't. It puts only my Python files in the library.zip, so my app doesn't work.

How do I make py2exe include my data files?

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

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

发布评论

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

评论(4

も星光 2024-09-06 18:56:55

我最终通过为 py2exe 提供选项 skip_archive=True 解决了这个问题。这导致它不会将 Python 文件放入 library.zip 中,而只是将其作为普通文件。然后我使用 data_files 将数据文件放入 Python 包中。

I ended up solving it by giving py2exe the option skip_archive=True. This caused it to put the Python files not in library.zip but simply as plain files. Then I used data_files to put the data files right inside the Python packages.

柠栀 2024-09-06 18:56:55

include_package_data 是一个 setuptools 选项,而不是 distutils 选项。在经典的 distutils 中,您必须使用 data_files = [] 指令自行指定数据文件的位置。 py2exe 是一样的。如果您有很多文件,可以使用 globos.walk 来检索它们。例如,请参阅 setup.py 所需的其他更改(数据文件添加)以创建像 MatPlotLib 这样的模块可以与 py2exe 一起使用。

还有一个邮件列表讨论是相关的。

include_package_data is a setuptools option, not a distutils one. In classic distutils, you have to specify the location of data files yourself, using the data_files = [] directive. py2exe is the same. If you have many files, you can use glob or os.walk to retrieve them. See for example the additional changes (datafile additions) required to setup.py to make a module like MatPlotLib work with py2exe.

There is also a mailing list discussion that is relevant.

浅黛梨妆こ 2024-09-06 18:56:55

这是我用来让 py2exe 将所有文件捆绑到 .zip 中的方法。请注意,要获取数据文件,您需要打开 zip 文件。 py2exe 不会为您重定向呼叫。

setup(windows=[target],
      name="myappname",
      data_files = [('', ['data1.dat', 'data2.dat'])],
      options = {'py2exe': {
        "optimize": 2,
        "bundle_files": 2, # This tells py2exe to bundle everything
      }},
)

py2exe 选项的完整列表位于此处

Here's what I use to get py2exe to bundle all of my files into the .zip. Note that to get at your data files, you need to open the zip file. py2exe won't redirect the calls for you.

setup(windows=[target],
      name="myappname",
      data_files = [('', ['data1.dat', 'data2.dat'])],
      options = {'py2exe': {
        "optimize": 2,
        "bundle_files": 2, # This tells py2exe to bundle everything
      }},
)

The full list of py2exe options is here.

我ぃ本無心為│何有愛 2024-09-06 18:56:55

我已经能够通过覆盖 py2exe 的函数之一,然后将它们插入到 py2exe 创建的 zip 文件中来做到这一点。

这是一个例子:

import py2exe
import zipfile

myFiles = [
    "C:/Users/Kade/Documents/ExampleFiles/example_1.doc",
    "C:/Users/Kade/Documents/ExampleFiles/example_2.dll",
    "C:/Users/Kade/Documents/ExampleFiles/example_3.obj",
    "C:/Users/Kade/Documents/ExampleFiles/example_4.H",
    ]

def better_copy_files(self, destdir):
    """Overriden so that things can be included in the library.zip."""

    #Run function as normal
    original_copy_files(self, destdir)

    #Get the zipfile's location
    if self.options.libname is not None:
        libpath = os.path.join(destdir, self.options.libname)

        #Re-open the zip file
        if self.options.compress:
            compression = zipfile.ZIP_DEFLATED
        else:
            compression = zipfile.ZIP_STORED
        arc = zipfile.ZipFile(libpath, "a", compression = compression)

        #Add your items to the zipfile
        for item in myFiles:
            if self.options.verbose:
                print("Copy File %s to %s" % (item, libpath))
            arc.write(item, os.path.basename(item))
        arc.close()

#Connect overrides
original_copy_files = py2exe.runtime.Runtime.copy_files
py2exe.runtime.Runtime.copy_files = better_copy_files

我从这里得到了这个想法,但不幸的是 py2exe改变了他们当时做事的方式。我希望这可以帮助别人。

I have been able to do this by overriding one of py2exe's functions, and then just inserting them into the zipfile that is created by py2exe.

Here's an example:

import py2exe
import zipfile

myFiles = [
    "C:/Users/Kade/Documents/ExampleFiles/example_1.doc",
    "C:/Users/Kade/Documents/ExampleFiles/example_2.dll",
    "C:/Users/Kade/Documents/ExampleFiles/example_3.obj",
    "C:/Users/Kade/Documents/ExampleFiles/example_4.H",
    ]

def better_copy_files(self, destdir):
    """Overriden so that things can be included in the library.zip."""

    #Run function as normal
    original_copy_files(self, destdir)

    #Get the zipfile's location
    if self.options.libname is not None:
        libpath = os.path.join(destdir, self.options.libname)

        #Re-open the zip file
        if self.options.compress:
            compression = zipfile.ZIP_DEFLATED
        else:
            compression = zipfile.ZIP_STORED
        arc = zipfile.ZipFile(libpath, "a", compression = compression)

        #Add your items to the zipfile
        for item in myFiles:
            if self.options.verbose:
                print("Copy File %s to %s" % (item, libpath))
            arc.write(item, os.path.basename(item))
        arc.close()

#Connect overrides
original_copy_files = py2exe.runtime.Runtime.copy_files
py2exe.runtime.Runtime.copy_files = better_copy_files

I got the idea from here, but unfortunately py2exe has changed how they do things sense then. I hope this helps someone out.

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