通用技术 pyinstaller 打包 python

kratos0517 · 2013年12月10日 · 1081 次阅读

有一个很好用,而且免费的工具 pyinstaller,支持 windows, linux, mac os, 并且支持 32bit, 64bit。
官网地址:
http://www.pyinstaller.org/

在 windows 下面,这个工具依赖 pywin32,所以使用这个工具之前需要先行安装 pywin32。如果是在 linux 或者 macos 下面就不需要了。
可以从下面的连接下载:
http://sourceforge.net/projects/pywin32/files/pywin32/Build%20217/

pywin32 是 python package,所以需要安装到 site-packages 这个文件中。

安装完成之后,我们下载 pyinstaller,这是个压缩文件,下载连接:
https://github.com/downloads/pyinstaller/pyinstaller/pyinstaller-2.0.zip

因为这不是 python package,所以不需要安装,直接解压的任何目录就可以工作了。解压之后需要配置 pyinstaller,cd 到你解压的文件夹,然后运行这个命令:
python Configure.py
其实就是写 config.dat 这个文件。

这个命令一般只需要执行一次,每次打包的时候不需要执行它。

更详细的资料请参考
http://www.pyinstaller.org/export/d3398dd79b68901ae1edd761f3fe0f4ff19cfb1a/project/doc/Manual.html

2 个命令完成一个简单的打包:
python Makespec.py [--onefile] yourprogram.py
python Build.py specfile

第一个命令是根据你提供的选项来生成一个工程的表述文件,包括编译选项,非常想 Makefile 文件,这里的--onefile 就是告诉 pyinstaller,我们想最终生成一个 exe 文件 (windows 下面)。specfile 就是第一条命令生成 spec 文件,这里需要指定全路径。
pyinstaller 还提供其他的一些选项:
-F, --onefile produce a single file deployment (see below).
-D, --onedir produce a single directory deployment (default).
-K, --tk include TCL/TK in the deployment.
-a, --ascii do not include encodings. The default (on Python versions with unicodesupport) is now to include all encodings.
-d, --debug use debug (verbose) versions of the executables.
-w, --windowed, --noconsole
Use the Windows subsystem executable, which does not openthe console when the program is launched.(Windows only)
-c, --nowindowed, --console
Use the console subsystem executable. This is the default. (Windows only)
-s, --strip the executable and all shared libraries will be run through strip. Notethat cygwin's strip tends to render normal Win32 dlls unusable.
-X, --upx if you have UPX installed (detected by Configure), this will use it tocompress your executable (and, on Windows, your dlls). See note below.
-o DIR,--out=DIR
create the spec file in directory. If not specified, and the currentdirectory is Installer's root directory, an output subdirectory will becreated. Otherwise the current directory is used.
-p DIR,--paths=DIR
set base path for import (like using PYTHONPATH). Multiple directories areallowed, separating them with the path separator (';' under Windows, ':'under Linux), or using this option multiple times.
--icon=
add file.ico to the executable's resources. (Windows only)
--icon=
add the n-th incon in file.exe to the executable's resources.(Windowsonly)
-v FILE,--version=FILE
add verfile as a version resource to the executable. (Windows only)
-n NAME,--name=NAME
optional name to assign to the project (from which the spec file name isgenerated). If omitted, the basename of the (first) script is used.

第二条命令就是根据刚才的生成工程描述文件,生成最终可执行程序。从某种角度来说,和 gcc 的编译非常像,第一条命令像是在 configure,第二个命令 像是 make。
第二条命令可以带 -O 选项 (大写的 O),来打开优化选项,进行编译打包。

python -O Build.py specfile

但是会提示你出错,那是因为我们在配置 pyinstaller 时,没有指定打开优化选项,所以我们配置命令变成:
python -O configure.py

你只需要输入你的程序入点文件,其他 pyinstaller 会帮你找。这里说入点文件就是你的 python 工程第一个要执行的文件,就像 c/c++ 工程中含有 main 函数的文件。在上面的例子中就是
yourprogam.py

这个工具会把所有的依赖的包给你打进去,包括 python 解释器,因此打成包,可以在没有安装 python 解释器的环境中运行。

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册