Python 脚本(.py)转换为可执行文件(exe)

使用 PyInstaller 将 Python 脚本转换为可执行文件(exe)

在开发和分发 Python 应用程序时,将脚本打包成可执行文件是一种常见的需求。
PyInstaller 是一个流行的工具,可以将 Python 脚本转换为独立的、可执行的二进制文件,从而使应用程序在没有 Python 解释器的环境中也能运行。本文将详细介绍如何使用 PyInstaller 进行转换。

1. 安装 PyInstaller

首先,确保已经安装了 Python 解释器。然后,可以使用 pip 来安装 PyInstaller:
第一步:查看是否安装pip环境:

pip --version

在这里插入图片描述
第二步:安装pyinstaller

pip install pyinstaller

在这里插入图片描述

2.创建 Python 脚本

在开始转换之前,首先需要编写要转换的 Python 脚本。可以根据自己的需求编写脚本,例如一个简单的 “hello.py” 脚本,输出 “Hello, World!”。

在这里插入图片描述

3.使用 PyInstaller 转换脚本

第一步:打开我们的python脚本存放目录,在目录框里面输入cmd按回车键并进入我们的cmd命令行界面。
在这里插入图片描述
第二步:我们在cmd命令行界面输入以下命令来对py文件进行转换:

pyinstaller --onefile hello.py

cmd会出现以下结果:

C:\ali>pyinstaller --onefile hello.py
154 INFO: PyInstaller: 3.6
154 INFO: Python: 3.8.6
155 INFO: Platform: Windows-10-10.0.25982-SP0
156 INFO: wrote C:\ali\hello.spec
162 INFO: UPX is not available.
164 INFO: Extending PYTHONPATH with paths
['C:\\ali', 'C:\\ali']
164 INFO: checking Analysis
164 INFO: Building Analysis because Analysis-00.toc is non existent
164 INFO: Initializing module dependency graph...
171 INFO: Caching module graph hooks...
184 INFO: Analyzing base_library.zip ...
2507 INFO: Processing pre-find module path hook   distutils
2509 INFO: distutils: retargeting to non-venv dir 'c:\\program files\\python\\lib'
5674 INFO: Caching module dependency graph...
5786 INFO: running Analysis Analysis-00.toc
5804 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\program files\python\python.exe
5947 WARNING: lib not found: api-ms-win-core-fibers-l1-1-0.dll dependency of C:\WINDOWS\system32\ucrtbase.dll
5953 WARNING: lib not found: api-ms-win-core-kernel32-legacy-l1-1-1.dll dependency of C:\WINDOWS\system32\ucrtbase.dll
5967 WARNING: lib not found: api-ms-win-core-sysinfo-l1-2-0.dll dependency of C:\WINDOWS\system32\ucrtbase.dll
5980 WARNING: lib not found: api-ms-win-core-fibers-l1-1-1.dll dependency of C:\WINDOWS\system32\ucrtbase.dll
6127 INFO: Analyzing C:\ali\hello.py
6129 INFO: Processing module hooks...
6129 INFO: Loading module hook "hook-distutils.py"...
6133 INFO: Loading module hook "hook-encodings.py"...
6227 INFO: Loading module hook "hook-lib2to3.py"...
6237 INFO: Loading module hook "hook-pydoc.py"...
6239 INFO: Loading module hook "hook-sysconfig.py"...
6241 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
6242 INFO: Loading module hook "hook-xml.py"...
6339 INFO: Loading module hook "hook-_tkinter.py"...
6574 INFO: checking Tree
6574 INFO: Building Tree because Tree-00.toc is non existent
6574 INFO: Building Tree Tree-00.toc
6717 INFO: checking Tree
6717 INFO: Building Tree because Tree-01.toc is non existent
6718 INFO: Building Tree Tree-01.toc
6747 INFO: Looking for ctypes DLLs
6767 INFO: Analyzing run-time hooks ...
6770 INFO: Including run-time hook 'pyi_rth__tkinter.py'
6772 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
6778 INFO: Looking for dynamic libraries
7074 INFO: Looking for eggs
7074 INFO: Using Python library c:\program files\python\python38.dll
7074 INFO: Found binding redirects:
[]
7077 INFO: Warnings written to C:\ali\build\hello\warn-hello.txt
7112 INFO: Graph cross-reference written to C:\ali\build\hello\xref-hello.html
7135 INFO: checking PYZ
7135 INFO: Building PYZ because PYZ-00.toc is non existent
7135 INFO: Building PYZ (ZlibArchive) C:\ali\build\hello\PYZ-00.pyz
7627 INFO: Building PYZ (ZlibArchive) C:\ali\build\hello\PYZ-00.pyz completed successfully.
7635 INFO: checking PKG
7635 INFO: Building PKG because PKG-00.toc is non existent
7635 INFO: Building PKG (CArchive) PKG-00.pkg
10023 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
10038 INFO: Bootloader c:\program files\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
10039 INFO: checking EXE
10039 INFO: Building EXE because EXE-00.toc is non existent
10039 INFO: Building EXE from EXE-00.toc
10041 INFO: Appending archive to EXE C:\ali\dist\hello.exe
10051 INFO: Building EXE from EXE-00.toc completed successfully.

在这里插入图片描述

这将生成一个名为 “hello.exe” 的可执行文件。我们存放hello.py文件原有的路径有一下几个文件:
在这里插入图片描述

4.运行可执行文件

转换完成后,可以在同一目录下的dist文件夹找到生成的可执行文件。双击运行 “hello.exe”,将会在命令行或终端窗口中看到 “Hello, World!” 的输出。
在这里插入图片描述

5.高级选项

PyInstaller 提供了许多高级选项,用于自定义转换过程。例如,可以指定生成的可执行文件的名称、图标、依赖项等。其他内容可以参考 PyInstaller 的官方文档以获取更多详细信息。

下面是 PyInstaller 的相关命令:

1. 安装 PyInstaller

在终端中输入以下命令安装 PyInstaller:

pip install pyinstaller
2. 打包 Python 代码

在终端中进入 Python 代码所在的目录,然后输入以下命令:

pyinstaller yourscript.py

其中,yourscript.py 是你要打包的 Python 代码文件名。
运行完这个命令后,PyInstaller 会自动创建一个 dist 目录,其中包含了可执行文件以及其他必要的文件。

3. 添加额外的文件

如果你的 Python 代码依赖于其他文件(如配置文件、图片等),则需要使用 --add-data 参数来将这些文件添加到可执行文件中,例如:

pyinstaller --add-data 'config.ini:.' yourscript.py

上述命令将会将 config.ini 文件添加到可执行文件中,并将其放置在可执行文件所在的目录下。

4. 指定输出目录

默认情况下,PyInstaller 会将可执行文件放置在 dist 目录中。如果你想将可执行文件放置在其他目录中,可以使用 --distpath 参数来指定输出目录,例如:

pyinstaller --distpath /path/to/output yourscript.py
5. 指定打包方式

PyInstaller 支持多种打包方式,包括单个文件、一系列文件和目录、或者一个 ZIP 包。你可以使用 --onefile 参数将 Python 代码打包成单个可执行文件,例如:

pyinstaller --onefile yourscript.py

如果你想将 Python 代码打包成一个 ZIP 包,则可以使用 --onedir 参数,例如:

pyinstaller --onedir yourscript.py
6. 其他参数

PyInstaller 还支持很多其他参数,如设置图标、禁止控制台窗口、设置 Python 版本等。你可以通过 pyinstaller --help 命令查看所有可用参数。

pyinstaller --icon=youricon.ico --noconsole yourscript.py

上述命令将会将 youricon.ico 设置为可执行文件的图标,并且禁止显示控制台窗口。

以上是 PyInstaller 的一些常用命令,可以根据实际情况进行调整。希望这篇博客能够帮助到你。

6.结论

使用 PyInstaller 将 Python 脚本转换为可执行文件是一种简单而有效的方式,使得应用程序能够在没有 Python 解释器的环境中独立运行。通过按照本文提供的步骤,你可以轻松地将自己的 Python 脚本转换为可执行文件。

希望这篇博客文档对你有所帮助!如果有任何问题,请随时提问。


猜你喜欢

转载自blog.csdn.net/m0_53157282/article/details/134063121