py2exe打包python文件成可执行程序

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 12:54   617   0

py2exe打包主要分为两大类:

(1)、普通python脚本程序打包,不包含图形界面库。此类程序打包我用了两种方法:

方法一、比较常见,网上大多用这种方法。代码实例如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# py2exe file
# 1.install py2exe application
# 2.python setup.py py2exe
from distutils.core import setup
import py2exe,sys,os
includes = ["encodings", "encodings.*"]

origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL

setup(console=[{'script':'DbCopy.py','icon_resources':[(1,"icon.ico")]}],
options={'py2exe': {'includes':['sip','ConfigParser','pymssql','cx_Oracle','decimal'],
"optimize": 0,
"includes": includes
#,"bundle_files": 1
}},
data_files=["icon.ico"])

方法二、

# -*- coding: utf-8-*-
from distutils.core import setup
from glob import glob
import py2exe
import os, sys
import shutil

if len(sys.argv) == 1:
sys.argv.append("py2exe")

includes = ["encodings", "encodings.*"]
options = {"py2exe":
{ "compressed": 1,
"optimize": 2,
"includes": includes,
"dist_dir": "bin",
"bundle_files": 1
}
}
setup(
version = "0.1",
description = u'runexe',
name = "runexe",
options = options,
zipfile = None,
console=[{"script": "runexe.py"}],
)
os.remove("bin//w9xpopen.exe")
shutil.rmtree("build")

以上两种方法基本一样,只是写法有点不同,用法也有点不同,

前面一种比较零活,适合用了多个驱动包的程序。后一种适合比较纯的python脚本,拿来改一下要打包的文件名就可以了。

(例如:我在用python连接mssql的时候,第二种方法一直没成功,后来用第一中方法很快解决了,但是我习惯用第二种)

(2)、包含图形界面(pyqt)的程序打包

#setup.py

# -*- coding: utf-8-*-

from distutils.core import setup
import py2exe,sys,os
includes = ["encodings", "encodings.*"]

origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ("QtSvg4.dll"):
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL

setup(windows=[{'script':'RunManager.py','icon_resources':[(1,"icon.ico")]}],
options={'py2exe': {'includes':['sip','PyQt4._qt','PyQt4.QtCore','PyQt4.QtSvg','PyQwt'],
"optimize": 0,
"includes": includes
#,"bundle_files": 1
}},
data_files=["icon.ico"])

以下是操作步骤:

1、Dos窗口进入本目录
2、运行命令 python setup.py py2exe --includes PyQt4.QtSvg,sip
3、生成文件在dist目录
4、需要手动拷贝到dist目录的文件:images(目录);app.init;打包相关文件中的PyQt4、qt.conf、QtSvg4.dll;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP