Python - 使用Pyinstaller将Python代码生成可执行文件

1 - Pyinstaller简介

Home-page: http://www.pyinstaller.org

PyInstaller是一个能够在多系统平台(Windows、*NIX、Mac OS)上将Python程序冻结(打包)为独立可执行文件的工具。

  • 可以捆绑所需的第三方库,并可与绝大多数常见的库和框架配合使用;
  • 可以与Python2.7和3.3-3.6协同工作,由于透明压缩而构建了更小的可执行文件;
  • 使用OS支持来加载动态库,从而确保完全兼容;

2 - Pyinstaller安装

$ pip3 install --proxy=10.144.1.10:8080 pyinstaller
Collecting pyinstaller
  Using cached PyInstaller-3.3.1.tar.gz
Requirement already satisfied: setuptools in c:\python36\lib\site-packages (from pyinstaller)
Collecting pefile>=2017.8.1 (from pyinstaller)
  Using cached pefile-2017.11.5.tar.gz
Collecting macholib>=1.8 (from pyinstaller)
  Using cached macholib-1.9-py2.py3-none-any.whl
Collecting future (from pyinstaller)
  Using cached future-0.16.0.tar.gz
Collecting pypiwin32 (from pyinstaller)
  Downloading pypiwin32-223-py3-none-any.whl
Collecting altgraph>=0.15 (from macholib>=1.8->pyinstaller)
  Using cached altgraph-0.15-py2.py3-none-any.whl
Collecting pywin32>=223 (from pypiwin32->pyinstaller)
  Downloading pywin32-223-cp36-cp36m-win_amd64.whl (9.0MB)
    100% |████████████████████████████████| 9.0MB 75kB/s
Installing collected packages: future, pefile, altgraph, macholib, pywin32, pypiwin32, pyinstaller
  Running setup.py install for future ... done
  Running setup.py install for pefile ... done
  Running setup.py install for pyinstaller ... done
Successfully installed altgraph-0.15 future-0.16.0 macholib-1.9 pefile-2017.11.5 pyinstaller-3.3.1 pypiwin32-223 pywin32-223

$ pip3 show pyinstaller
Name: PyInstaller
Version: 3.3.1
Summary: PyInstaller bundles a Python application and all its dependencies into a single package.
Home-page: http://www.pyinstaller.org
Author: Giovanni Bajo, Hartmut Goebel, David Vierra, David Cortesi, Martin Zibricky
Author-email: [email protected]
License: GPL license with a special exception which allows to use PyInstaller to build and distribute non-free programs (including commercial ones)
Location: c:\python36\lib\site-packages
Requires: setuptools, pefile, macholib, future

3 - 使用Pyinstaller

3-1 文件及目录

通过“pyinstaller [...] scriptname”方式运行,将会默认生成以下文件及目录:

.spec

  • 打包配置文件,事后可删除
  • 控制参数:--specpath DIR Folder to store the generated spec file (default: current directory)
  • 使用pyinstaller时,默认会在当前的路径下生成一个以spec为后缀的打包配置文件,pyinstaller依据spec文件的内容来创建exe文件;
  • 正常情况下,不需要去修改这个spec文件,除非需要打包其他数据文件;

build/

  • 存放过程文件,事后可删除
  • 控制参数:--workpath WORKPATH Where to put all the temporary work files, .log, .pyz and etc. (default: .\build)

dist/

  • 存放捆绑的应用程序,包含生成好的exe文件
  • 控制参数:--distpath DIR Where to put the bundled app (default: .\dist)

3-2 常用命令选项

--distpath DIR        # 指定捆绑应用程序的存放路径和目录
--workpath WORKPATH        # 指定存放临时文件的路径和目录
-y, --noconfirm        # 无询问覆盖目录
--clean        # 清除缓存文件
--log-level LEVEL        # 指定日志等级

-D, --onedir        # 生成一个包含exe文件和依赖文件的目录
-F, --onefile        # 只生成一个包含依赖的exe格式文件
--specpath DIR        # 指定存放spec文件的目录
-n NAME, --name NAME        # 指定输出文件名称(默认为第一个文件名)
-c, --console, --nowindowed        # 使用控制台(命令行窗口)作为标准IO(默认)
-w, --windowed, --noconsole        # 不使用控制台(命令行窗口)作为标准IO(不适用*NIX系统)
-i <FILE.ico or FILE.exe,ID or FILE.icns>, --icon <FILE.ico or FILE.exe,ID or FILE.icns>    # 指定应用ico图标

--add-data <SRC;DEST or SRC:DEST>        # !
--add-binary <SRC;DEST or SRC:DEST>        # !
-p DIR, --paths DIR        # 指定搜索路径,用‘;‘分割多个路径
--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        # 加密python字节码

4 - Pyinstaller示例

4-1 基本用法

python代码

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ cat GUI_Tkinter.py
#! python3
# -*- coding: utf-8 -*-
from tkinter import *
import tkinter.messagebox as messagebox

class Application(Frame):  # 从Frame派生一个Application类,这是所有Widget的父容器
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()  # pack()方法把Widget加入到父容器中,并实现布局
        self.createWidgets()

    def createWidgets(self):
        self.helloLabel = Label(self, text=‘This is a sample.‘)  # 创建一个Label
        self.helloLabel.pack()
        self.nameInput = Entry(self)
        self.nameInput.pack()
        self.alertButton = Button(self, text=‘Hi‘, command=self.hello)  # 创建一个Button,点击按钮,触发hello()
        self.alertButton.pack()
        self.quitButton = Button(self, text=‘Quit‘, command=self.quit)  # 点击按钮,触发self.quit(),程序退出
        self.quitButton.pack()

    def hello(self):
        name = self.nameInput.get() or ‘world‘  # 获得用户输入的文本
        messagebox.showinfo(‘Message‘, ‘Hello, %s‘ % name)  # 弹出消息对话框

app = Application()  # 实例化Application
app.master.title(‘Hello World‘)  # 设置窗口标题
app.mainloop()  # 主消息循环

# ### Tkinter
# Python内置的是支持Tk的Tkinter,无需安装任何包,可以直接使用,能够满足基本的GUI程序的要求;
# 如果是非常复杂的GUI程序,建议用操作系统原生支持的语言和库来编写;
# 常见的第三方图形界面库包括Tk、wxWidgets、Qt、GTK等;

生成可执行文件

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ ls -l
total 4
-rwxr-xr-x 1 guowli 1049089 1574 Feb 11 09:31 GUI_Tkinter.py*

[email protected] MINGW64 /d/Anliven-Running/TempTest
$

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ pyinstaller -F -w GUI_Tkinter.py
205 INFO: PyInstaller: 3.3.1
207 INFO: Python: 2.7.12
207 INFO: Platform: Windows-7-6.1.7601-SP1
208 INFO: wrote D:\Anliven-Running\TempTest\GUI_Tkinter.spec
217 INFO: UPX is not available.
220 INFO: Extending PYTHONPATH with paths
[‘D:\\Anliven-Running\\TempTest‘, ‘D:\\Anliven-Running\\TempTest‘]
223 INFO: checking Analysis
223 INFO: Building Analysis because out00-Analysis.toc is non existent
223 INFO: Initializing module dependency graph...
236 INFO: Initializing module graph hooks...
365 INFO: running Analysis out00-Analysis.toc
392 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
  required by c:\python27\python.exe
2701 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
2705 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
2710 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_acd19a1fe1da248a.manifest
2806 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4940_none ...
2806 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_08e4299fa83d7e3c.manifest
2809 INFO: Searching for file msvcr90.dll
2809 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_08e4299fa83d7e3c\msvcr90.dll
2810 INFO: Searching for file msvcp90.dll
2810 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_08e4299fa83d7e3c\msvcp90.dll
2812 INFO: Searching for file msvcm90.dll
2812 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_08e4299fa83d7e3c\msvcm90.dll
2908 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
2909 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
2911 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_acd19a1fe1da248a.manifest
2912 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 4940)
3044 INFO: Caching module hooks...
3070 INFO: Analyzing D:\Anliven-Running\TempTest\GUI_Tkinter.py
8287 INFO: Processing pre-safe import module hook   _xmlplus
8536 INFO: Processing pre-find module path hook   distutils
9615 INFO: Loading module hooks...
9615 INFO: Loading module hook "hook-distutils.py"...
9618 INFO: Loading module hook "hook-sysconfig.py"...
9619 INFO: Loading module hook "hook-xml.py"...
9769 INFO: Loading module hook "hook-httplib.py"...
9770 INFO: Loading module hook "hook-_tkinter.py"...
10214 INFO: checking Tree
10217 INFO: Building Tree because out00-Tree.toc is non existent
10217 INFO: Building Tree out00-Tree.toc
10470 INFO: checking Tree
10470 INFO: Building Tree because out01-Tree.toc is non existent
10470 INFO: Building Tree out01-Tree.toc
10498 INFO: Loading module hook "hook-encodings.py"...
11378 INFO: Looking for ctypes DLLs
11409 INFO: Analyzing run-time hooks ...
11412 INFO: Including run-time hook ‘pyi_rth__tkinter.py‘
11421 INFO: Looking for dynamic libraries
11786 INFO: Looking for eggs
11787 INFO: Using Python library C:\WINDOWS\system32\python27.dll
11787 INFO: Found binding redirects:
[BindingRedirect(name=u‘Microsoft.VC90.CRT‘, language=None, arch=u‘amd64‘, oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 4940), publicKeyToken=u‘1fc8b3b9a1e18e3b‘)]
11796 INFO: Warnings written to D:\Anliven-Running\TempTest\build\GUI_Tkinter\warnGUI_Tkinter.txt
11839 INFO: Graph cross-reference written to D:\Anliven-Running\TempTest\build\GUI_Tkinter\xref-GUI_Tkinter.html
12016 INFO: checking PYZ
12016 INFO: Building PYZ because out00-PYZ.toc is non existent
12017 INFO: Building PYZ (ZlibArchive) D:\Anliven-Running\TempTest\build\GUI_Tkinter\out00-PYZ.pyz
12598 INFO: Building PYZ (ZlibArchive) D:\Anliven-Running\TempTest\build\GUI_Tkinter\out00-PYZ.pyz completed successfully.
12664 INFO: checking PKG
12664 INFO: Building PKG because out00-PKG.toc is non existent
12664 INFO: Building PKG (CArchive) out00-PKG.pkg
12828 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 4940)
16441 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
16570 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
16570 INFO: checking EXE
16572 INFO: Building EXE because out00-EXE.toc is non existent
16572 INFO: Building EXE from out00-EXE.toc
16573 INFO: Appending archive to EXE D:\Anliven-Running\TempTest\dist\GUI_Tkinter.exe
16603 INFO: Building EXE from out00-EXE.toc completed successfully.

[email protected] MINGW64 /d/Anliven-Running/TempTest
$

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ ls -l
total 8
drwxr-xr-x 1 guowli 1049089    0 Feb 27 15:09 build/
drwxr-xr-x 1 guowli 1049089    0 Feb 27 15:09 dist/
-rwxr-xr-x 1 guowli 1049089 1574 Feb 11 09:31 GUI_Tkinter.py*
-rw-r--r-- 1 guowli 1049089  767 Feb 27 15:09 GUI_Tkinter.spec

[email protected] MINGW64 /d/Anliven-Running/TempTest
$

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ ls -lR
.:
total 8
drwxr-xr-x 1 guowli 1049089    0 Feb 27 15:09 build/
drwxr-xr-x 1 guowli 1049089    0 Feb 27 15:09 dist/
-rwxr-xr-x 1 guowli 1049089 1574 Feb 11 09:31 GUI_Tkinter.py*
-rw-r--r-- 1 guowli 1049089  767 Feb 27 15:09 GUI_Tkinter.spec

./build:
total 4
drwxr-xr-x 1 guowli 1049089 0 Feb 27 15:09 GUI_Tkinter/

./build/GUI_Tkinter:
total 10400
-rw-r--r-- 1 guowli 1049089    1351 Feb 27 15:09 GUI_Tkinter.exe.manifest
-rw-r--r-- 1 guowli 1049089  118100 Feb 27 15:09 out00-Analysis.toc
-rw-r--r-- 1 guowli 1049089   91969 Feb 27 15:09 out00-EXE.toc
-rw-r--r-- 1 guowli 1049089 8490727 Feb 27 15:09 out00-PKG.pkg
-rw-r--r-- 1 guowli 1049089   90661 Feb 27 15:09 out00-PKG.toc
-rw-r--r-- 1 guowli 1049089 1396916 Feb 27 15:09 out00-PYZ.pyz
-rw-r--r-- 1 guowli 1049089   28133 Feb 27 15:09 out00-PYZ.toc
-rw-r--r-- 1 guowli 1049089   80981 Feb 27 15:09 out00-Tree.toc
-rw-r--r-- 1 guowli 1049089    6778 Feb 27 15:09 out01-Tree.toc
-rw-r--r-- 1 guowli 1049089    2866 Feb 27 15:09 warnGUI_Tkinter.txt
-rw-r--r-- 1 guowli 1049089  322575 Feb 27 15:09 xref-GUI_Tkinter.html

./dist:
total 8560
-rwxr-xr-x 1 guowli 1049089 8762599 Feb 27 15:09 GUI_Tkinter.exe*

[email protected] MINGW64 /d/Anliven-Running/TempTest
$

运行可执行文件

4-2 自定义ico图标

图标下载:https://www.easyicon.net/

生成可执行文件

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ pyinstaller -F -w -i panda.ico GUI_Tkinter.py
279 INFO: PyInstaller: 3.3.1
279 INFO: Python: 2.7.12
279 INFO: Platform: Windows-7-6.1.7601-SP1
280 INFO: wrote D:\Anliven-Running\TempTest\GUI_Tkinter.spec
......
......
......
13562 INFO: Appending archive to EXE D:\Anliven-Running\TempTest\dist\GUI_Tkinter.exe
13578 INFO: Building EXE from out00-EXE.toc completed successfully.

[email protected] MINGW64 /d/Anliven-Running/TempTest
$

[email protected] MINGW64 /d/Anliven-Running/TempTest
$ ls -lR
.:
total 76
drwxr-xr-x 1 guowli 1049089     0 Feb 27 15:39 build/
drwxr-xr-x 1 guowli 1049089     0 Feb 27 15:40 dist/
-rwxr-xr-x 1 guowli 1049089  1574 Feb 11 09:31 GUI_Tkinter.py*
-rw-r--r-- 1 guowli 1049089   785 Feb 27 15:39 GUI_Tkinter.spec
-rw-r--r-- 1 guowli 1049089 67646 Feb 27 15:37 panda.ico

./build:
total 4
drwxr-xr-x 1 guowli 1049089 0 Feb 27 15:40 GUI_Tkinter/

./build/GUI_Tkinter:
total 10400
-rw-r--r-- 1 guowli 1049089    1351 Feb 27 15:40 GUI_Tkinter.exe.manifest
-rw-r--r-- 1 guowli 1049089  118100 Feb 27 15:40 out00-Analysis.toc
-rw-r--r-- 1 guowli 1049089   91976 Feb 27 15:40 out00-EXE.toc
-rw-r--r-- 1 guowli 1049089 8490727 Feb 27 15:40 out00-PKG.pkg
-rw-r--r-- 1 guowli 1049089   90661 Feb 27 15:40 out00-PKG.toc
-rw-r--r-- 1 guowli 1049089 1396916 Feb 27 15:40 out00-PYZ.pyz
-rw-r--r-- 1 guowli 1049089   28133 Feb 27 15:40 out00-PYZ.toc
-rw-r--r-- 1 guowli 1049089   80981 Feb 27 15:40 out00-Tree.toc
-rw-r--r-- 1 guowli 1049089    6778 Feb 27 15:40 out01-Tree.toc
-rw-r--r-- 1 guowli 1049089    2866 Feb 27 15:40 warnGUI_Tkinter.txt
-rw-r--r-- 1 guowli 1049089  322575 Feb 27 15:40 xref-GUI_Tkinter.html

./dist:
total 8620
-rwxr-xr-x 1 guowli 1049089 8826599 Feb 27 15:40 GUI_Tkinter.exe*

[email protected] MINGW64 /d/Anliven-Running/TempTest
$

运行可执行文件

5 - Tips

5-1 onefolder模式

参数:-D, --onedir # 生成一个包含exe文件和依赖文件的目录

onefolder模式可以用于确认打包是否完整正确。

打包成onefile文件时,发布前最好先打包成onefolder,检查一下需要的文件是否被正确打包。

5-2 打包IDE编写的python文件

在类似Pycharm导入的模块可能并没有安装到本地的python环境,就会导致在python文件在PyCharm中能正常运行但在命令行运行报错的现象。

在使用pyinstaller打包时,对应的现象就是:执行EXE文件闪退,或提示ImportError错误。

此问题只需要使用pip安装相应模块再打包即可。

5-3 加密python字节码

利用加密参数 --key ,可自定义一个16位密钥来加密pyc文件,加大反编译难度。

注意:必须事先安装PyCrypto(https://pypi.python.org/pypi/pycrypto/)。

官方文档:https://pyinstaller.readthedocs.io/en/stable/usage.html#encrypting-python-bytecode

To encrypt the Python bytecode modules stored in the bundle, pass the --key=key-string argument on the command line.

For this to work, you must have the PyCrypto module installed. The key-string is a string of 16 characters which is used to encrypt each file of Python byte-code before it is stored in the archive inside the executable file.

5-4 文件名称及路径

  • 文件路径中不能出现空格和英文句号;
  • 源文件必须是UTF-8编码,暂不支持其他编码类型;

5-5 报错信息

如果执行生成的exe文件闪退,没有报错信息,可以尝试在cmd命令中运行exe文件,可能会看到报错原因。

5-6 关于跨平台

虽然Pyinstaller是跨平台的,但是打包之后的exe文件并不能跨平台执行。

在Windows系统下打包生成的exe文件只能在Windows系统下运行,同理在Linux下打包生成的exe文件也只能在Linux下运行。

如果将在Windows系统下打包生成的exe文件在Linux下运行,将会报错“Exec format error. Binary file not executable”。

6 - 帮助信息

-h, --help # 显示帮助信息

$ pyinstaller -h
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] [-s] [--noupx] [-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] [--distpath DIR]
                   [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a]
                   [--clean] [--log-level LEVEL]
                   scriptname [scriptname ...]

positional arguments:
  scriptname            name of scriptfiles to be processed or exactly one
                        .spec-file. If a .spec-file is specified, most options
                        are unnecessary and are ignored.

optional arguments:
  -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 LEVEL     Amount of detail in build-time console messages. LEVEL
                        may be one of TRACE, DEBUG, INFO, WARN, ERROR,
                        CRITICAL (default: INFO).

What to generate:
  -D, --onedir          Create a one-folder bundle containing an executable
                        (default)
  -F, --onefile         Create a one-file bundled executable.
  --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:
  --add-data <SRC;DEST or SRC:DEST>
                        Additional non-binary files or folders to be added to
                        the executable. The path separator is platform
                        specific, ``os.pathsep`` (which is ``;`` on Windows
                        and ``:`` on most unix systems) is used. This option
                        can be used multiple times.
  --add-binary <SRC;DEST or SRC:DEST>
                        Additional binary files to be added to the executable.
                        See the ``--add-data`` option for more details. This
                        option can be used multiple times.
  -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, --hiddenimport 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 (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             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 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.

原文地址:https://www.cnblogs.com/anliven/p/8481515.html

时间: 2024-10-31 09:10:29

Python - 使用Pyinstaller将Python代码生成可执行文件的相关文章

pyinstaller 模块-python文件生成exe可执行文件

安装 PyInstaller 模块与安装其他 Python 模块一样,使用 pip 命令安装即可.在命令行输入如下命令: pip install pyinstaller 在 PyInstaller 模块安装成功之后,在 Python 的安装目录下的 Scripts(D:\Python\Python36\Scripts) 目录下会增加一个 pyinstaller.exe 程序,接下来就可以使用该工具将 Python 程序生成 EXE 程序了. pyinstaller 选项 Python 源文件 不

pyinstaller打包python源程序访问hive

1.需求 使用hvie server一段时间后,业务部门需要自己不定时的查询业务数据,之前这一块都是他们提需求我们来做,后来发现这样重复一样的工作放在我们这边做是在没有效率,遂提出给他们工具或者web UI自助查询,当然hive有自己的hwi可以通过网页UI进行自助查询,但是这对不懂sql的业务人员有点不太友好,目前有没时间去修改hwi的UI,所以还是给他们提供查询工具吧.我这边主要使用python thrift访问集群的hive,所以自然要将python源码打包成.exe,业务人员在windo

使用PyInstaller打包Python程序

使用PyInstaller打包Python程序 PyInstaller介绍: PyInstaller是一个能将Python程序转换成单个可执行文件的程序, 操作系统支持Windows, Linux, Mac OS X, Solaris和AIX.并且很多包都支持开箱即用,不依赖环境. 环境为windows7操作系统,python2.7.8 virtual environment 官网:https://github.com/pyinstaller/pyinstaller 详细步骤: 1. win7下

pyinstaller 还原python代码的方法

pyinstaller 的作用就是将python打包成对应平台的可执行文件.一般这种可执行文件的体积都比较大. 我们可以先通过逆向软件查看一下具体信息 查看字符串信息 只要有诸如以上的字符串 就说明这个可执行文件有很大程度上是通过pyinstaller打包的 解密方法有两个 1. pyinstxtractor.py 方法很简单 直接python pyinstxtractor.py 待解密的文件 当然有可能会报错,不过绝大多数情况下是可以使用的 2. pyi-archive_viewer 通过查看

使用pyinstaller将python文件打包成exe文件

pyinstaller官方网页:http://www.pyinstaller.org/ 官方文档:https://pyinstaller.readthedocs.io/en/stable/ pyinstaller支持Python 2.7和Python 3.3+版本,并且支持Windows(XP+).Mac OS X(10.7+)和Linux,但是只能在对应的系统编译对应的程序. 1.pyinstaller安装 在cmd(命令提示符)中直接安装 1 pip install pyinstaller

python(11)---pyinstaller

今天就记录一下,pyinstaller的安装,python是可以在命令行下进行运行的,有时候我们需要 将python的源代码进行打包放到不同的平台下进行运行,pyinstaller就是执行此功能的.网上写了 安装过程中碰到的一些问题,我就记录我在安装过程中碰到的一些问题,方便后来的人如果遇到 之后可以少走一些弯路. 我是windows10下进行安装的,首先“Win+R”打开window终端,如果是python2输入: pip2  install --user pyinstaller:如果是pyt

python基础教程:python的环境搭建

Python编程语言可应用于多平台包括 Linux 和 Mac OS X.一般的 Linux 发行版本都自带 Python,Mac OS X 最新版也自带了 Python,也就是已经安装好了,不需要再配置. Windows 下直接下载最新版的 Python 2.7.9,安装的时候注意选择 你可以通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Python的安装版本. Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, Su

Python之路,Day1 - Python基础1

本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语句 表达式for 循环 break and continue 表达式while 循环 作业需求 一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语

Python之路,Day1 - Python基础1---转自金角大王

本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语句 表达式for 循环 break and continue 表达式while 循环 作业需求 一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语