简介安装与说明

pyinstall官网
http://www.111cn.net/phper/python/122937.htm

使用

命令格式为

1
pyinstaller [options] script [script ...] | specfile

所以最基本的使用为:

1
pyinstaller myscript.py

  • Writes myscript.spec in the same folder as the script.
  • Creates a folder build in the same folder as the script if it does not exist.
  • Writes some log files and working files in the build folder.
  • Creates a folder dist in the same folder as the script if it does not exist.
  • Writes the myscript executable folder in the dist folder.

更深入的使用得配合各种选项和myscript.spec文件

选项

Spec 文件

UPX压缩可执行包

默认生成的python可执行文件有点大,可以通过upx程序进行压缩,upx程序的主页位于:https://upx.github.io/ ,目前windows版的最新upx为upx391w.zip ,下载后,将其中的upx.exe文件放到C:\Python27目录下(python可执行程序的安装目录),再次执行的时候,可以跟上–upx进行压缩。默认存在时不指定也会进行压缩,生成exe文件时,在其日志中可以发现如下内容:

###加密 python 可执行文件
Encrypting Python Bytecode

跨平台打包

windows 安装包的版本文件

pyi-grab_version 命令
pyi-grab_version executable_with_version_resource

1
2
3
4
5
6
7
8
9
--version-file='file_version_info.txt',
--version-file file_version_info.txt,
icon
icon='news_xishou.ico',
import logging
try:
1/0
except Exception as e:
logging.exception("message")

运行时信息

当应用运行时,有可能从以下三个地方获取可访问的文件:

  1. 当前程序的文件夹内,比如我们添加的 Adding Data Files
  2. 用户在使用时放置在程序文件夹内
  3. 用户当前工作目录

使用 file and sys._MEIPASS

在Python中,__file__ 表示当前脚本文件的路径.当程序包文件运行时,加载器会设置sys.frozen变量和
把运行文件的绝对路径保存在 sys._MEIPASS 变量中;

在 一个文件夹安装包的模式,这个路径指向文件夹;
在一个文件的模式中,这个路径指向 _MEIxxxxxx 临时文件夹

使用 sys.executable and sys.argv[0]

在 python 运行时, sys.executable 表示当前程序的路径,一般为python解释的路径,如:
C:\Python27\python.exe

但是在 安装包运行,虽然也表示运行程序的路径,但已经不是 python解释器的路径,而是程序加载器
bootloader
所以这个变量提供了一个可靠的路径标识当前用户使用的路径位置;

当用户是使用快捷键运行时,sys.argv[0]标识快捷键的位置,sys.executable仍然是可执行文件真实存在的位置

当需要提供不同快捷键运行不同功能时,就可以使用该参数
os.path.basename(sys.argv[0])

示例程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python3
import sys, os
frozen = 'not'
if getattr(sys, 'frozen', False):
# we are running in a bundle
frozen = 'ever so'
bundle_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))
print( 'we are',frozen,'frozen')
print( 'bundle dir is', bundle_dir )
print( 'sys.argv[0] is', sys.argv[0] )
print( 'sys.executable is', sys.executable )
print( 'os.getcwd is', os.getcwd() )

LD_LIBRARY_PATH / LIBPATH considerations

在类Unix系统中,表示库搜索路径

使用 Spec 文件

使用spec文件的场景:

  1. 打包数据文件
  2. 加载运行时库文件
  3. 增加Python运行时配置参数
  4. 打包跨程序的安装包

单独生成spec文件

1
pyi-makespec options name.py [other scripts ...]

当生成并修改好spec文件后,运行

1
pyinstaller options name.spec

当你使用spec文件时,绝大多数的选项参数已经在spec文件中给出,这些参数不会改变;也就说你在命令行中给出的选项参数会被spec文件的参数覆盖,不起作用

只有下述几个参数才是有意义的
–upx-dir=
–distpath=
–workpath=
–noconfirm
–ascii

spec 文件的结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
block_cipher = None
a = Analysis(['minimal.py'],
pathex=['/Developer/PItests/minimal'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=None,
runtime_hooks=None,
excludes=None,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,... )
coll = COLLECT(...)

由四部分组成: Analysis, PYZ, EXE and COLLECT.

向压缩包中添加文件

添加文件

1
2
3
4
5
6
7
8
added_files = [
( '/mygame/sfx/*.mp3', 'sfx' ),
( 'src/README.txt', '.' )
]
a = Analysis(...
datas = added_files,
...
)

可以添加整个文件夹

1
2
3
4
5
added_files = [
( '/mygame/data', 'data' ),
( '/mygame/sfx/*.mp3', 'sfx' ),
( 'src/README.txt', '.' )
]

/mygame/data整个文件夹复制到data目录

在模块中使用 data文件

pkgutils.get_data()

1
2
import pkgutil
help_bin = pkgutil.get_data( 'helpmod', 'help_data.txt' )

添加可执行文件

增加Python运行时参数

1
2
3
v to write a message to stdout each time a module is initialized.
u for unbuffered stdio.
W and an option to change warning behavior: W ignore or W once or W error.