简体   繁体   English

py2exe,可执行文件:如何将数据打包成单个文件?

[英]py2exe, executable: How to pack data into single file?

I am wondering of how to pack several files into one single windows executable file with py2exe.我想知道如何使用 py2exe 将多个文件打包成一个 Windows 可执行文件。 I could get all python files (dll's, ...) into one py2exe.我可以将所有 python 文件(dll 的,...)放入一个 py2exe 中。 But how can i copy some of my own files (ie images or configuration files) into my executable and load them into python with a relative path.但是如何将我自己的一些文件(即图像或配置文件)复制到我的可执行文件中,并使用相对路径将它们加载到 python 中。 In Example:在示例中:

file = open("config.txt",r")
file2 = open("mysubfolder\config.txt","r")

I don't know if you can embed your image files into the exe.我不知道你是否可以将你的图像文件嵌入到exe中。 If there is a way, I'm also interested.如果有办法,我也有兴趣。 What I know is that you can use them from a separate folder.我所知道的是,您可以从单独的文件夹中使用它们。 For this, you need to modify your setup file.为此,您需要修改安装文件。 Add a data path for the thing you want to load.为要加载的事物添加数据路径。 For example, add this to your setup file:例如,将其添加到您的安装文件中:

import glob

data_files = [('mysubfolder', glob.glob('mysubfolder/config.txt'))]

setup(
   data_files = data_files,
   windows = [GUI2Exe_Target_1]
)

Of course, you can add more folders or files to data_files.当然,您可以在 data_files 中添加更多文件夹或文件。 And for other configuration options in this setup file, you can refer to the py2exe official site.而对于这个设置文件中的其他配置选项,你可以参考 py2exe 官方网站。

If you are using PyQT (or PySide), it's possible to compile resource files into python modules ( .py files).如果您使用 PyQT(或 PySide),则可以将资源文件编译为 Python 模块( .py文件)。 That way, you just need to import them to be able to use them, and py2exe will include them in the executable by default.这样,您只需要import它们就可以使用它们,默认情况下 py2exe 会将它们包含在可执行文件中。

Look at the PyQt4 Resource System .查看PyQt4 资源系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM