小工具知识网

当前位置: 首页 > 行业知识 > Python 脚本(.py)转换为可执行文件(exe)

行业知识

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

2026-11-07 02:05:26 来源:互联网转载

使用 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.py154 INFO: PyInstaller: 3.6154 INFO: Python: 3.8.6155 INFO: Platform: Windows-10-10.0.25982-SP0156 INFO: wrote C:\ali\hello.spec162 INFO: UPX is not available.164 INFO: Extending PYTHONPATH with paths['C:\ali', 'C:\ali']164 INFO: checking Analysis164 INFO: Building Analysis because Analysis-00.toc is non existent164 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   distutils2509 INFO: distutils: retargeting to non-venv dir 'c:\program files\python\lib'5674 INFO: Caching module dependency graph...5786 INFO: running Analysis Analysis-00.toc5804 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable  required by c:\program files\python\python.exe5947 WARNING: lib not found: api-ms-win-core-fibers-l1-1-0.dll dependency of C:\WINDOWS\system32\ucrtbase.dll5953 WARNING: lib not found: api-ms-win-core-kernel32-legacy-l1-1-1.dll dependency of C:\WINDOWS\system32\ucrtbase.dll5967 WARNING: lib not found: api-ms-win-core-sysinfo-l1-2-0.dll dependency of C:\WINDOWS\system32\ucrtbase.dll5980 WARNING: lib not found: api-ms-win-core-fibers-l1-1-1.dll dependency of C:\WINDOWS\system32\ucrtbase.dll6127 INFO: Analyzing C:\ali\hello.py6129 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 Tree6574 INFO: Building Tree because Tree-00.toc is non existent6574 INFO: Building Tree Tree-00.toc6717 INFO: checking Tree6717 INFO: Building Tree because Tree-01.toc is non existent6718 INFO: Building Tree Tree-01.toc6747 INFO: Looking for ctypes DLLs6767 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 libraries7074 INFO: Looking for eggs7074 INFO: Using Python library c:\program files\python\python38.dll7074 INFO: Found binding redirects:[]7077 INFO: Warnings written to C:\ali\build\hello\warn-hello.txt7112 INFO: Graph cross-reference written to C:\ali\build\hello\xref-hello.html7135 INFO: checking PYZ7135 INFO: Building PYZ because PYZ-00.toc is non existent7135 INFO: Building PYZ (ZlibArchive) C:\ali\build\hello\PYZ-00.pyz7627 INFO: Building PYZ (ZlibArchive) C:\ali\build\hello\PYZ-00.pyz completed successfully.7635 INFO: checking PKG7635 INFO: Building PKG because PKG-00.toc is non existent7635 INFO: Building PKG (CArchive) PKG-00.pkg10023 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.10038 INFO: Bootloader c:\program files\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe10039 INFO: checking EXE10039 INFO: Building EXE because EXE-00.toc is non existent10039 INFO: Building EXE from EXE-00.toc10041 INFO: Appending archive to EXE C:\ali\dist\hello.exe10051 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 设置为可执行文件的图标,并且禁止显示控制台窗口。

6.结论

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

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


上一篇:怎么购买服务器硬盘?

下一篇:萤火突击怎么去外服服务器?