📌 步骤 1:安装 pyinstaller
在终端(CMD 或 PowerShell)运行:
pip install pyinstaller
📌 步骤 2:使用 pyinstaller
进行打包
假设你的 Python 脚本名为 monitor.py
,你可以运行以下命令进行打包:
pyinstaller --onefile --noconsole monitor.py
🔹 说明
--onefile
:打包成单个.exe
文件--noconsole
:打包后运行时不显示黑色的命令行窗口(如果你的程序需要输出日志到终端,可以去掉这个参数)
📌 步骤 3:找到生成的 .exe
文件
执行完 pyinstaller
命令后,终端会输出一些信息。你的 .exe
文件会出现在 dist
目录下:
dist\monitor.exe
你可以直接运行这个 monitor.exe
进行测试。
📌 步骤 4:优化打包
如果你的 monitor.py
依赖 psutil
,有些 .dll
文件可能不会自动打包,可以尝试:
pyinstaller --onefile --noconsole --hidden-import psutil monitor.py
📌 步骤 5:打包后文件整理
打包完成后,你的目录结构可能如下:
/monitor_project /dist monitor.exe ← 你的可执行文件 /build /monitor.spec monitor.py
你可以将 dist/monitor.exe
拷贝到其他目录运行,不需要 Python 解释器 也能正常执行。
📌 进阶
如果你希望: ✅ 图标:添加 .ico
图标:
pyinstaller --onefile --noconsole --icon=your_icon.ico monitor.py
✅ 指定输出目录:
pyinstaller --onefile --distpath ./output monitor.py