python程序打包成.exe----pyinstaller工具

1. 环境

windows

2. 安装

准备文件:PyWin32 or pypiwin32

运行如下安装命令:  pip install pyinstaller==3.0

不要使用3.2版本,编译完成后会报Runtime Error, R6034错误.

3.0版本无此问题。

3. 打包

把.py或.pyw文件拷贝到pyinstaller所在目录

执行命令: pyinstaller -F xxx.py ,会把生成的文件自动放到dist目录下,文件名默认为xxx.exe。

常用参数说明:

-F:把所有文件打包成一个.exe。

-D:把所有文件打包成一个目录,里面包含可执行文件和所需库等其他文件。

-n NAME:重新命名可执行文件。

-w:打开打包后的程序,将不会弹出命令行窗口。

4. 单独文件和目录比较

目录的比单个文件尺寸要大;

目录的比单个文件打开速度要快。

在WIN7上打包,放到XP上,目录的打开失败,单个文件正常。

详细参数说明见文档

http://pythonhosted.org/PyInstaller/#general-options

General Options

-h, --help show this help message and exit
-v, --version Show program version info and exit.
--distpath=DIR Where to put the bundled app (default: ./dist)
--workpath=WORKPATH
  Where to put all the temporary work files, .log, .pyz and etc. (default: ./build)
-y, --noconfirm
  Replace output directory (default: SPECPATH/dist/SPECNAME) without asking for confirmation
--upx-dir=UPX_DIR
  Path to UPX utility (default: search the execution path)
-a, --ascii Do not include unicode encoding support (default: included if available)
--clean Clean PyInstaller cache and remove temporary files before building.
--log-level=LOGLEVEL
  Amount of detail in build-time console messages (default: INFO, choose one of DEBUG, INFO, WARN, ERROR, CRITICAL)

What to generate

-F, --onefile Create a one-file bundled executable.
-D, --onedir Create a one-folder bundle containing an executable (default)
--specpath=DIR Folder to store the generated spec file (default: current directory)
-n NAME, --name=NAME
  Name to assign to the bundled app and spec file (default: first script's basename)

What to bundle, where to search

-p DIR, --paths=DIR
  A path to search for imports (like using PYTHONPATH). Multiple paths are allowed, separated by ':', or use this option multiple times
--hidden-import=MODULENAME
  Name an import not visible in the code of the script(s). This option can be used multiple times.
--additional-hooks-dir=HOOKSPATH
  An additional path to search for hooks. This option can be used multiple times.
--runtime-hook=RUNTIME_HOOKS
  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 (his Python names,not path names) that will be ignored (as thoughit was not found).This option can be used multiple times.
--key=KEY The key used to encrypt Python bytecode.

How to generate

-d, --debug Tell the bootloader to issue progress messages while initializing and starting the bundled app. Used to diagnose problems with missing imports.
-s, --strip Apply a symbol-table strip to the executable and shared libs (not recommended for Windows)
--noupx Do not use UPX even if it is available (works differently between Windows and *nix)

Windows and Mac OS X specific options

-c, --console, --nowindowed
  Open a console window for standard i/o (default)
-w, --windowed, --noconsole
  Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle.This option is ignored in *NIX systems.
-i <FILE.ico or FILE.exe,ID or FILE.icns>, --icon=<FILE.ico or FILE.exe,ID or FILE.icns>
  FILE.ico: apply that icon to a Windows executable. FILE.exe,ID, extract the icon with ID from an exe. FILE.icns: apply the icon to the .app bundle on Mac OS X

Windows specific options

--version-file=FILE
  add a version resource from FILE to the exe
-m <FILE or XML>, --manifest=<FILE or XML>
  add manifest FILE or XML to the exe
-r <FILE[,TYPE[,NAME[,LANGUAGE]]]>, --resource=<FILE[,TYPE[,NAME[,LANGUAGE]]]>
  Add or update a resource of the given type, name and language from FILE to a Windows executable. 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.
上一篇:获得用户的真实IP地址


下一篇:python 回溯法 子集树模板 系列 —— 19、野人与传教士问题