如何将所有资源编译成一个可执行文件?

发布于 2024-11-19 17:51:36 字数 1541 浏览 2 评论 0原文

我用 python 编写了 GTK 应用程序。

所有图形用户界面都在glade文件中,并且使用了一些图像。我希望将我的应用程序编译成可执行文件。为此,我使用 PyInstaller 编译器和 UPX 打包程序。

我已经按照手册所述完成了:

python Configure.py
python Makespec.py --onefile --windowed --upx /path/to/yourscript.py
python Build.py /path/to/yourscript.spec

PyInstaller 工作完美并创建一个 exe 文件。但为了使我的应用程序正常工作,我必须将我的 gladeimage 文件复制到 exe 文件夹中。

有什么方法可以将这些文件编译为可执行文件吗?

我已经用各种方式编辑了我的规范文件,但我无法实现我想要的。下面的规范文件仅将文件复制到目录,但不编译为可执行文件

# -*- mode: python -*-
a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'), os.path.join(HOMEPATH,'support\\useUnicode.py'), 'r:\\connection\\main.py'],
             pathex=['C:\\Documents and Settings\\Lixas\\Desktop\\pyinstaller-1.5-rc1'])

pyz = PYZ(a.pure)

exe = EXE( pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'NetworkChecker.exe'),
          debug=False,
          strip=False,
          upx=True,
          console=False,
          icon='r:\\connection\\ikona.ico'        )

coll = COLLECT(
    exe,
    [('gui.glade', 'r:\\connection\\gui.glade', 'DATA')],
    [('question16.png', 'r:\\connection\\question16.png', 'DATA')],
#   a.binaries,
#   strip=False,
    upx=True,
    name='distFinal')

我希望只有一个可执行文件,其中包含所有内容

I've wrote GTK application with python.

All graphical user interface is in glade file, and there are some images used. I wish to compile my application into EXEcutable file. For that I'm using PyInstaller compiler and UPX packer.

I've done as manual says:

python Configure.py
python Makespec.py --onefile --windowed --upx /path/to/yourscript.py
python Build.py /path/to/yourscript.spec

PyInstaller works perfectly and create one exe file. But to make my application work correctly i have to copy my glade and image files into exe's folder.

Is there any way to compile those files into executable?

I've edited my spec file in various ways but i can not achieve what i want. Spec file below only copies file to directory, but does not compile into executable file

# -*- mode: python -*-
a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'), os.path.join(HOMEPATH,'support\\useUnicode.py'), 'r:\\connection\\main.py'],
             pathex=['C:\\Documents and Settings\\Lixas\\Desktop\\pyinstaller-1.5-rc1'])

pyz = PYZ(a.pure)

exe = EXE( pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'NetworkChecker.exe'),
          debug=False,
          strip=False,
          upx=True,
          console=False,
          icon='r:\\connection\\ikona.ico'        )

coll = COLLECT(
    exe,
    [('gui.glade', 'r:\\connection\\gui.glade', 'DATA')],
    [('question16.png', 'r:\\connection\\question16.png', 'DATA')],
#   a.binaries,
#   strip=False,
    upx=True,
    name='distFinal')

I wish to have only one executable file with everything included into

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

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

发布评论

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

评论(3

德意的啸 2024-11-26 17:51:36

PyInstaller确实允许您将所有资源捆绑到 exe 中,而无需将数据文件转换为 .py 文件 - 您的 COLLECT 对象似乎是正确的,棘手的步骤是访问这些文件运行时。 PyInstaller 会将它们解压到临时目录中,并通过 _MEIPASS2 变量告诉您​​它们的位置。为了获取开发模式和打包模式下的文件路径,我使用以下命令:

def resource_path(relative):
    return os.path.join(
        os.environ.get(
            "_MEIPASS2",
            os.path.abspath(".")
        ),
        relative
    )


# in development
>>> resource_path("gui.glade")
"/home/shish/src/my_app/gui.glade"

# in deployment
>>> resource_path("gui.glade")
"/tmp/_MEI34121/gui.glade"

PyInstaller does allow you to bundle all your resources into the exe, without the trickyness of turning your data files into .py files -- your COLLECT object seems to be correct, the tricky step is accessing these files at runtime. PyInstaller will unpack them into a temporary directory and tell you where they are with the _MEIPASS2 variable. To get the file paths in both development and packed mode, I use this:

def resource_path(relative):
    return os.path.join(
        os.environ.get(
            "_MEIPASS2",
            os.path.abspath(".")
        ),
        relative
    )


# in development
>>> resource_path("gui.glade")
"/home/shish/src/my_app/gui.glade"

# in deployment
>>> resource_path("gui.glade")
"/tmp/_MEI34121/gui.glade"
妥活 2024-11-26 17:51:36

通过一些更改,您可以将所有内容合并到源代码中,从而合并到可执行文件中。

如果您运行 gdk-pixbuf-csource 在图像文件上,您可以将它们转换为字符串,然后可以使用 gtk.gdk.pixbuf_new_from_inline ()

您还可以将 Glade 文件作为字符串包含在程序中,然后使用 gtk.Builder.add_from_string()

With a few changes, you can incorporate everything into your source code and thus into your executable file.

If you run gdk-pixbuf-csource on your image files, you can convert them into strings, which you can then load using gtk.gdk.pixbuf_new_from_inline().

You can also include your Glade file as a string in the program and then load it using gtk.Builder.add_from_string().

舟遥客 2024-11-26 17:51:36

有什么方法可以将这些文件编译成可执行文件吗?

严格来说:不,因为你编译源代码,而林间空地文件是XML,图像是二进制数据。您通常要做的是创建一个安装程序(即,一个自解压存档,当安装程序运行时,它将把不同的文件放置在正确的目录中)。

编辑:如果您关心的只是拥有一个单文件可执行文件(因此这与“编译”无关,而实际上与永久写入文件系统上的文件数量有关),您可以尝试使用< a href="http://www.py2exe.org/index.cgi/SingleFileExecutable" rel="nofollow">此脚本基于 py2exe。它的作用是在每次运行程序时创建临时文件,并在执行完成时删除它们。

编辑2:显然,您所要求的也可以在PyInstaller下实现。来自文档

默认情况下,pyinstaller.py 创建一个包含主可执行文件和动态库的分发目录。选项 --onefile 指定您希望 PyInstaller 构建一个包含所有内容的单个文件。

Is there any way to compile those files into executable?

Strictly speaking: no, because you compile source code, while the glade file is XML and the images are binary data. What you would normally do is to create an installer (i.e. a self-unpacking archive that will place different files in the correct directories when the installer is ran).

EDIT: If your concern is simply to have a one-file executable (so it's not about "compiling" but really about the number of files that are permanently written on the filesystem) you could try to use this script based on py2exe. What it does, is to create temporary files each time the program is ran, removing them when execution is completed.

EDIT2: Apparently what you are asking for is also possible under PyInstaller. From the documentation:

By default, pyinstaller.py creates a distribution directory containing the main executable and the dynamic libraries. The option --onefile specifies that you want PyInstaller to build a single file with everything inside.

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