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

Posted Anliven - DevOpsNew

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: pyinstaller@googlegroups.com
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代码

guowli@5CG450158J 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等;

生成可执行文件

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

guowli@5CG450158J MINGW64 /d/Anliven-Running/TempTest
$

guowli@5CG450158J 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.

guowli@5CG450158J MINGW64 /d/Anliven-Running/TempTest
$

guowli@5CG450158J 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

guowli@5CG450158J MINGW64 /d/Anliven-Running/TempTest
$

guowli@5CG450158J 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*

guowli@5CG450158J MINGW64 /d/Anliven-Running/TempTest
$

运行可执行文件

4-2 自定义ico图标

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

生成可执行文件

guowli@5CG450158J 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.

guowli@5CG450158J MINGW64 /d/Anliven-Running/TempTest
$

guowli@5CG450158J 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*

guowli@5CG450158J 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.

以上是关于Python - 使用Pyinstaller将Python代码生成可执行文件的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu下使用pyinstaller将python文件打包成exe(py文件中含有keras框架的代码)

Python利用pyinstaller工具将py文件打包成.exe可执行文件

将 pyinstaller 与两个 python 环境一起使用

将python文件(.py)转换为可执行文件(.exe)的方法

pyinstaller---将py文件打包成exe

python桌面应用(pyinstaller打包多个py文件)