pyinstaller的使用
2020-05-04 本文已影响0人
sunnnnnnnnnny
pyinstaller简介
pyinstaller是一个常用的python脚本打包工具,可以将python脚本打包成可执行文件,方便在没有python环境的系统中使用
使用方法
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
[--add-data <SRC;DEST or SRC:DEST>]
[--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
[--hidden-import MODULENAME]
[--additional-hooks-dir HOOKSPATH]
[--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
[--key KEY] [-d {all,imports,bootloader,noarchive}] [-s]
[--noupx] [--upx-exclude FILE] [-c] [-w]
[-i <FILE.ico or FILE.exe,ID or FILE.icns>]
[--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
[--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
[--win-no-prefer-redirects]
[--osx-bundle-identifier BUNDLE_IDENTIFIER]
[--runtime-tmpdir PATH] [--bootloader-ignore-signals]
[--distpath DIR] [--workpath WORKPATH] [-y]
[--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
scriptname [scriptname ...]
必需的参数:
scriptname 处理的脚本名 或者.spec-file文件名
选项:
-h, --help 显示帮助文档并退出
-v, --version 显示版本信息并退出.
--distpath DIR 设置生成文件的目录 (缺省为 .\dist)
--workpath WORKPATH 设置临时文件目录, .log, .pyz and etc. (default: .\build)
-y, --noconfirm 替换输出目录 (缺省为 SPECPATH\dist\SPECNAME) ,不需要确认
--upx-dir UPX_DIR UPX的目录 (缺少会搜索可执行目录)
-a, --ascii 不包含unicode 编辑支持 (默认会包含)
--clean 在生成可执行文件前会清理pyinstaller缓存和临时文件
--log-level LEVEL build过程中的日志级别. 包括5个级别,TRACE, DEBUG, INFO, WARN, ERROR,CRITICAL (默认为 INFO).
指定生成的文件:
-D, --onedir 生成一个目录,包括一个可执行文件(默认)
-F, --onefile 只生成生成一个文件
--specpath DIR 指定spec目录,缺省为当前目录
-n NAME, --name NAME 指定可执行文件的名称,缺少为脚本的名称
What to bundle, where to search:
--add-data <SRC;DEST or SRC:DEST> 指定需要包含到目标文件中的非二进制文件,这个选项可以被执行多次,src;dest为windows,src:dest为linux
--add-binary <SRC;DEST or SRC:DEST> 指定需要包含到目标文件中的二进制文件,这个选项可以被执行多次,src;dest为windows,src:dest为linux
-p DIR, --paths DIR 指定import目录(like using PYTHONPATH),可使用;隔开,或使用多次该选项
--hidden-import MODULENAME, --hiddenimport MODULENAME 指定不需要引入的包,可使用多次
--additional-hooks-dir HOOKSPATH 添加hooks目录,可使用多次
--runtime-hook RUNTIME_HOOKS 自定义运行时hook文件,
Path to a custom runtime hook file. A runtime hook is
code that is bundled with the executable and is
executed before any other code or module to set up
special features of the runtime environment. This
option can be used multiple times.
--exclude-module EXCLUDES
Optional module or package (the Python name, not the
path name) that will be ignored (as though it was not
found). This option can be used multiple times.
--key KEY 用来加密二进制码的key
如何生成:
-d {all,imports,bootloader,noarchive}, --debug {all,imports,bootloader,noarchive}
Provide assistance with debugging a frozen
application. This argument may be provided multiple
times to select several of the following options.
- all: All three of the following options.
- imports: specify the -v option to the underlying
Python interpreter, causing it to print a message
each time a module is initialized, showing the
place (filename or built-in module) from which it
is loaded. See
https://docs.python.org/3/using/cmdline.html#id4.
- bootloader: tell the bootloader to issue progress
messages while initializing and starting the
bundled app. Used to diagnose problems with
missing imports.
- noarchive: instead of storing all frozen Python
source files as an archive inside the resulting
executable, store them as files in the resulting
output directory.
-s, --strip Apply a symbol-table strip to the executable and
shared libs (not recommended for Windows)
--noupx 不使用noupx (works
differently between Windows and *nix)
--upx-exclude FILE Prevent a binary from being compressed when using upx.
This is typically used if upx corrupts certain
binaries during compression. FILE is the filename of
the binary without path. This option can be used
multiple times.
Windows and Mac OS X specific options:
-c, --console, --nowindowed 打开一个标准的控制台窗口
-w, --windowed, --noconsole 不显示console窗口.在 *NIX 系统中该选项没有用.
-i <FILE.ico or FILE.exe,ID or FILE.icns>, --icon <FILE.ico or FILE.exe,ID or FILE.icns>
FILE.ico: 为windows可执行文件指定图标.
FILE.exe,ID, 从一个exe中根据ID提取出icon
FILE.icns: 在MAC os系统中指定app文件的icon
Windows specific options:
--version-file FILE 指定version resource文件
-m <FILE or XML>, --manifest <FILE or XML>
add manifest FILE or XML to the exe
-r RESOURCE, --resource RESOURCE
Add or update a resource to a Windows executable. The
RESOURCE is one to four items,
FILE[,TYPE[,NAME[,LANGUAGE]]]. FILE can be a data file
or an exe/dll. For data files, at least TYPE and NAME
must be specified. LANGUAGE defaults to 0 or may be
specified as wildcard * to update all resources of the
given TYPE and NAME. For exe/dll files, all resources
from FILE will be added/updated to the final
executable if TYPE, NAME and LANGUAGE are omitted or
specified as wildcard *.This option can be used
multiple times.
--uac-admin Using this option creates a Manifest which will
request elevation upon application restart.
--uac-uiaccess Using this option allows an elevated application to
work with Remote Desktop.
Windows Side-by-side Assembly searching options (advanced):
--win-private-assemblies
Any Shared Assemblies bundled into the application
will be changed into Private Assemblies. This means
the exact versions of these assemblies will always be
used, and any newer versions installed on user
machines at the system level will be ignored.
--win-no-prefer-redirects
While searching for Shared or Private Assemblies to
bundle into the application, PyInstaller will prefer
not to follow policies that redirect to newer
versions, and will try to bundle the exact versions of
the assembly.
Mac OS X specific options:
--osx-bundle-identifier BUNDLE_IDENTIFIER
Mac OS X .app bundle identifier is used as the default
unique program name for code signing purposes. The
usual form is a hierarchical name in reverse DNS
notation. For example:
com.mycompany.department.appname (default: first
script's basename)
Rarely used special options:
--runtime-tmpdir PATH
Where to extract libraries and support files in
`onefile`-mode. If this option is given, the
bootloader will ignore any temp-folder location
defined by the run-time OS. The ``_MEIxxxxxx``-folder
will be created here. Please use this option only if
you know what you are doing.
--bootloader-ignore-signals
Tell the bootloader to ignore signals rather than
forwarding them to the child process. Useful in
situations where e.g. a supervisor process signals
both the bootloader and child (e.g. via a process
group) to avoid signalling the child twice.
打包注意事项
- python文件中尽量使用from * import *
- 不要在anaconda环境下打包,生成的目标文件会非常大
参考资料
https://blog.csdn.net/bearstarx/article/details/81054134
https://blog.csdn.net/weixin_39000819/article/details/80942423
https://pyinstaller.readthedocs.io/en/stable/