将Python代码编译成单个exe [英] Compiling python code into a single exe

查看:674
本文介绍了将Python代码编译成单个exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我已经尝试将python代码编译成一个单独的exe,我没有设法正确地做。

    我试过pyinstaller,这是.spec文件:

     # -  *  -  mode:python  -  -  
    a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'),os.path.join(HOMEPATH,'support\\\useUnicode.py'), programs\\\toolbox.py'],
    pathex = ['C:\\Users\\Ronaldo\\Desktop\\Python\\pyinstaller'])
    pyz = PYZ(a.pure)
    exe = EXE(pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a .datas,
    name = os.path.join('dist','program.exe'),
    debug = False,
    strip = False,
    upx = True,
    console = False)

    Pyinstaller编译得很好,创建了一个单独的exe文件, b $ b,但显然没有安装(2.7.2)
    的相同版本的其他用户无法运行它。不给任何错误,它根本不启动。


  2. 试过py2exe。当涉及到编译(与所有库
    旁边,和其余的文件)它工作正常。我试过
    编译它到一个单一的文件,但编译的exe文件给了我一个
    toolbox.exe已停止工作的错误。这里是错误报告,
    ,以防万一,可能是有用的:

     问题签名:
    问题事件名称:APPCRASH
    应用程序名称:toolbox.exe
    应用程序版本:0.0.0.0
    应用程序时间戳:49180193
    故障模块名称:StackHash_0a9e
    故障模块版本:0.0。 0.0
    故障模块时间戳:00000000
    异常代码:c0000005
    异常偏移量:01b61fcb
    OS版本:6.1.7601.2.1.0.256.1
    语言标识ID:1033
    附加信息1:0a9e
    其他信息2:0a9e372d3b4ad19135b953a78882e789
    其他信息3:0a9e
    其他信息4:0a9e372d3b4ad19135b953a78882e789

    setup.py文件是:

      from distutils.core import setup 
    import py2exe,sys,os

    sys.argv.append('py2exe')

    setup(
    options = {'py2exe':{ 'bundle_files':1}},
    windows = [{'script':toolbox.py}],
    zipfile = None,
    )single.py文件:

    下面是single.py:

      import os,sys,ctypes 

    ctypes.windll.user32.MessageBoxA(0,
    curdir:%s\\\
    exedir:%s\\\
    sys.winver :%s%(
    os.path.abspath(os.curdir),
    os.path.abspath(os.path.dirname(sys.argv [0])),
    sys.winver,
    ),%s - Message%os.path.basename(sys.executable),0x30


我写的程序使用tkinter,sys,random和win32clipboard(pywin)模块。
我做错了什么?
是否有其他更好的编译器?



更新:通过Joël的提示,我使用调试和控制台模式编译。仍然没有为没有python 2.7的用户工作。这是错误讯息:

  C:\Users\XXXXX\Desktop> program.exe 
找到嵌入式PKG:C:\Users\XXXXX\Desktop\program.exe
提取二进制文件
设置运行子
设置魔术环境var
创建子进程
等待子进程完成...
找到嵌入的PKG:C:\Users\XXXXX\Desktop\program.exe
工作路径:C:/ Users / XXXXX / AppData / Local / Temp / _MEI14042 /
manifestpath:C:/Users/XXXXX/AppData/Local/Temp/_MEI14042/program.exe.manifest
创建的激活上下文
激活上下文激活
C:\Users\XXXXX\AppData\Local\Temp\MEI14042\python27.dll
操作环境
PYTHONPATH = C:/ Users / XXXXX / AppData / Local / Temp / _MEI14042; C:/ Users / XXXXX / Desktop
从CArchive导入模块
解压缩iu
解压缩struct
解压缩归档
安装导入钩子
outPYZ1。 pyz

我真的希望这可以帮助解决可能的问题。


解决方案

我的两分钱:你使用 debug PyInstaller



只需更新您的spec档案:

  exe = EXE 
[...]
debug = True,
[...]


b $ b

(注意:为了避免多个消息框,您可能需要设置控制台输出: console = True



也许它会返回一些有用的数据。请在这种情况下提供输出(如果有)。






根据输出结果,一个常见的问题,因为如果PyInstaller解决你的模块的依赖,依赖的依赖可能会被遗忘。



在您的情况下,缺少Tcl ,某些 / code> library:您应该看看这里: Python,Pyinstaller创建快捷方式在窗口中



根据文档:


在Makespec .py,这是支持的命令行:python Makespec.py [opts] [...]在哪里允许选项是:



[...] >

-K,--tk在部署中包含TCL / TK。


可以尝试使用此参数,并检查对您的 spec 文件的影响。我敢打赌,这是在分析或EXE函数中考虑的模块中的一个补充。


I've been trying to compile python code into a single exe, and i didn't manage to do it correctly.

  1. I've tried pyinstaller, and this is the .spec file:

    # -*- mode: python -*-
    a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'), os.path.join(HOMEPATH,'support\\useUnicode.py'), 'programs\\toolbox.py'],
             pathex=['C:\\Users\\Ronaldo\\Desktop\\Python\\pyinstaller'])
    pyz = PYZ(a.pure)
    exe = EXE( pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'program.exe'),
          debug=False,
          strip=False,
          upx=True,
          console=False )
    

    Pyinstaller compiled fine, created a single exe file which worked, but apparently other users that didn't have the same version of python installed (2.7.2) couldn't run it. Doesn't give any error, It simply doesn't launch. I've tried it by sending it to a few friends, and also tried on a virtual machine.

  2. Tried py2exe. When it comes to compiling (with all the libraries next to it, and the rest of files) it works fine. I tried compiling it to a single file but the compiled exe file gave me a " toolbox.exe has stopped working" error. Here's the error report, just in case, might be useful:

    Problem signature:
      Problem Event Name:   APPCRASH
      Application Name: toolbox.exe
      Application Version:  0.0.0.0
      Application Timestamp:    49180193
      Fault Module Name:    StackHash_0a9e
      Fault Module Version: 0.0.0.0
      Fault Module Timestamp:   00000000
      Exception Code:   c0000005
      Exception Offset: 01b61fcb
      OS Version:   6.1.7601.2.1.0.256.1
      Locale ID:    1033
      Additional Information 1: 0a9e
      Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
      Additional Information 3: 0a9e
      Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
    

    The setup.py file is:

    from distutils.core import setup
    import py2exe, sys, os
    
    sys.argv.append('py2exe')
    
    setup(
        options = {'py2exe': {'bundle_files': 1}},
        windows = [{'script': "toolbox.py"}],
        zipfile = None,
    ) single.py file:
    

    And here's the single.py:

    import os, sys, ctypes
    
    ctypes.windll.user32.MessageBoxA(0,
        "curdir: %s\nexedir: %s\nsys.winver: %s" % (
            os.path.abspath(os.curdir),
            os.path.abspath(os.path.dirname(sys.argv[0])),
            sys.winver,
        ), "%s - Message" % os.path.basename(sys.executable), 0x30
    )
    

The program I wrote uses tkinter, sys, random and win32clipboard(pywin) modules. What am I doing wrong? Are there any other, better compilers?

Update: By Joël's tip, I compiled with debug, and with console mode. Still didn't work for users that don't have python 2.7. This is the error message:

C:\Users\XXXXX\Desktop>program.exe
Found embedded PKG: C:\Users\XXXXX\Desktop\program.exe
Extracting binaries
Setting up to run child
Setting magic environment var
Creating child process
Waiting for child process to finish...
Found embedded PKG: C:\Users\XXXXX\Desktop\program.exe
workpath: C:/Users/XXXXX/AppData/Local/Temp/_MEI14042/
manifestpath: C:/Users/XXXXX/AppData/Local/Temp/_MEI14042/program.exe.manifest
Activation context created
Activation context activated
C:\Users\XXXXX\AppData\Local\Temp\_MEI14042\python27.dll
Manipulating evironment
PYTHONPATH=C:/Users/XXXXX/AppData/Local/Temp/_MEI14042;C:/Users/XXXXX/Desktop
importing modules from CArchive
extracted iu
extracted struct
extracted archive
Installing import hooks
outPYZ1.pyz

I really hope this serves as help for the possible answer.

解决方案

My two cents: did you make a test using the debug option of PyInstaller?

Just update your specfile:

exe = EXE( pyz,
           [...]
           debug=True,
           [...])

(Note: in order to avoid numerous message boxes, you may want to set console output: console=True)

Maybe it would return some useful data. Please provide us with output in this case (if there's any).


According to the output you get, this is quite a common problem, because if PyInstaller resolves dependencies for your modules, dependencies of dependencies may be forgotten.

In your case, Tcl is missing, and this is needed by some Tkinter library: you should take a look here: Python, Pyinstaller creating shortcuts in windows

According to documentation:

Elaborating on Makespec.py, this is the supported command line: python Makespec.py [opts] [ ...] Where allowed OPTIONS are:

[...]

-K, --tk include TCL/TK in the deployment.

You may make a try with this argument, and check the impact on your spec file. I bet it's an addition in the modules taken into account in Analysis or in the EXE function.

这篇关于将Python代码编译成单个exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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