python打包成exe
目前有三种方法可以实现python打包成exe,分别为
- py2exe
- Pyinstaller
- cx_Freeze
其中没有一个是完美的
1.py2exe的话不支持egg类型的python库
2.Pyinstaller2.1打包成单独的exe后不支持中文路径,最新版的在win10下打包的exe不可以在之前版本的windows上运行。不过还好的是2.0版本支持中文路径,不过2.0版本不支持安装,需要单独使用
3.cx_Freeze无法打包成单独的exe,只能打包成msi安装文件
1.py2exe
关于py2exe,首先要注意的是安装的版本,因为即使你用的是python2的pip来安装py2exe,它还是会给你装上py2exe 0.9xx ,而这个版本是python3使用的,python2 会报错。python2的兼容版本是0.6的
操作方法:
1.首先就是把你写的一堆py文件放到一个文件夹中,如果你写的是窗口程序,也不需要把后缀改成pyw
2.然后在目录下建一个setup.py文件:
gui程序如下
from distutils.core import setup
import py2exe
import sys
#this allows to run it with a simple double click.
sys.argv.append('py2exe')
py2exe_options = {
includes: [sip],
dll_excludes: [MSVCP90.dll,],
compressed: 1,
optimize: 2,
ascii: 0
}
setup(
name = 'E_hentai Downloader',
version = '1.0',
windows = ['E_hentai.py',],
zipfile = None,
options = {'py2exe': py2exe_options}
)
控制台如下
from distutils.core import setup
import py2exe
import sys
# this allows to run it with a simple double click.
sys.argv.append('py2exe')
py2exe_options = {
includes: [sip],
dll_excludes: [MSVCP90.dll, ],
compressed: 1,
optimize: 2,
ascii: 0,
bundle_files: 1
}
setup(
name='Anhona Downloader',
version='1.0',
console=[AnhonaDownloader.py],
zipfile=None,
options={'py2exe': py2exe_options}
)
上面的option选项中还可以加上一句 bundle_files: 1 这个代表打包成一个单文件,需要注意的是,在打包pyqt的时候,最好不要加上这个选项,因为这个相当于静态编译,不 会去连接外面的库,所以会导致编译出来后无法加载jpg图片,即使你添加了addLibraryPath。
3.在命令行里面运行 python setup.py py2exe 即可打包完工(最好进入当前文件夹,这样可以在当前目录生成exe)
附加: 如果你是打包的pyqt,记得把plugins拷贝到应用程序目录下,包括pyqt4目录下的qt.conf(等同于addLibraryPath)
2.pyinstaller
这个比较容易使用,如果是控制台程序支持输入下面的命令
pyinstaller –console –onefile –icon=my.ico xxxxx.py
3.cx_freeze
这个和py2exe比较像,需要一个setup.py文件,内容如下
# -*- coding: utf-8 -*-
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {packages: [os], excludes: [tkinter]}
# GUI applications require a different base on Windows (the default is for a
# console application).
setup(name=AnhonaDownloader,
version=1.0,
description=My GUI application!,
options={build_exe: build_exe_options},
executables=[Executable(AnhonaDownloader.py)])
然后输入
setup.py build
如果需要作出安装包需要加上bdist_msi
微信赞赏
支付宝赞赏