如何将所有资源编译成一个可执行文件? [英] How to compile all resources into one executable file?

查看:261
本文介绍了如何将所有资源编译成一个可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

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



strong>是否有任何方法可以将这些文件编译为可执行文件?



我已经以各种方式编辑我的spec文件,但我无法实现我想要的。下面的Spec文件只将文件复制到目录,但不会编译成可执行文件

 # -  *  -  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和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')



只有一个包含

解决方案

PyInstaller 的一个可执行文件允许您捆绑所有资源进入exe,没有把你的数据文件转换为.py文件的棘手 - 你的COLLECT对象似乎是正确的,棘手的步骤是在运行时访问这些文件。 PyInstaller会将它们解包到一个临时目录中,并告诉你它们在_MEIPASS2变量中的位置。要获得开发和打包模式下的文件路径,我使用这个:

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




#正在开发中
>>>> resource_path(gui.glade)
/home/shish/src/my_app/gui.glade

在部署中使用
>>>> resource_path(gui.glade)
/tmp/_MEI34121/gui.glade


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

解决方案

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"

这篇关于如何将所有资源编译成一个可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆