python自带编译器如何生成exe,将Python代码编译成单个exe

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

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值