让 py2exe 包含我的数据文件(如 include_package_data)
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终通过为 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 inlibrary.zip
but simply as plain files. Then I useddata_files
to put the data files right inside the Python packages.include_package_data
是一个 setuptools 选项,而不是 distutils 选项。在经典的 distutils 中,您必须使用data_files = []
指令自行指定数据文件的位置。py2exe
是一样的。如果您有很多文件,可以使用glob
或os.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 thedata_files = []
directive.py2exe
is the same. If you have many files, you can useglob
oros.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.
这是我用来让 py2exe 将所有文件捆绑到 .zip 中的方法。请注意,要获取数据文件,您需要打开 zip 文件。 py2exe 不会为您重定向呼叫。
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.
The full list of py2exe options is here.
我已经能够通过覆盖 py2exe 的函数之一,然后将它们插入到 py2exe 创建的 zip 文件中来做到这一点。
这是一个例子:
我从这里得到了这个想法,但不幸的是 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:
I got the idea from here, but unfortunately py2exe has changed how they do things sense then. I hope this helps someone out.