PyInstaller 是一个将 Python 应用程序打包成独立可执行文件的工具。下面是一个详细的教程,介绍如何使用 PyInstaller 将你的 Python 脚本打包成 .exe 文件。
1. 安装 PyInstaller
首先,你需要安装 PyInstaller。可以使用 pip 进行安装:
pip install pyinstaller
2. 编写一个简单的 Python 脚本
编写一个简单的 Python 脚本,假设文件名为 hello.py
:
print("Hello, world!")
3. 使用 PyInstaller 打包
使用 PyInstaller 打包这个脚本:
pyinstaller hello.py
这个命令将生成以下文件和文件夹:
build/
文件夹:包含临时文件,打包过程中使用。dist/
文件夹:包含生成的可执行文件。hello.spec
文件:包含打包配置,可以用于定制打包过程。
4. 生成一个独立的可执行文件
如果你希望生成一个独立的可执行文件,可以使用 --onefile
选项:
pyinstaller --onefile hello.py