创建虚拟环境打包python脚本

1. 创建 Conda 虚拟环境

假设你要用 Python 3.10:

conda create -n exceltools python=3.9.19 -y

激活环境:

conda activate exceltools

2. 安装依赖

你的脚本用到 pandasopenpyxlpyinstaller

pip install  pandas==2.1.4  
pip install  openpyxl==3.0.10  
pip install  xlrd==2.0.1     

pip install pyinstaller===5.13

导航到脚本所在的目录

使用cd命令切换到你的 Python 脚本(如excel_gui.py)所在的文件夹:

. 测试脚本

确认在虚拟环境里能正常运行

python excel_gui.py

4. 打包成 EXE

在虚拟环境里执行:

pyinstaller --onefile --noconsole excel_gui.py
  • --onefile → 打成单 exe 文件
  • --noconsole → 去掉黑色命令行窗口(如果你用 Tkinter/WinForms GUI)

打包完成后,在 dist/ 目录下会生成 excel_gui.exe

5. 脱离环境运行

  • 复制 dist/excel_gui.exe 到任何 Windows 机器上,双击即可运行。
  • 不需要目标机安装 Python 或 Anaconda。

6. 如果你要连同虚拟环境依赖一起打包

有时候 PyInstaller 找不到某些库(比如 Numpy、Pandas 相关),可以指定隐藏导入:

pyinstaller --onefile --noconsole --hidden-import pandas --hidden-import openpyxl excel_gui.py

⚡ 小结:

查看当前 Conda 的环境存储路径:

conda config --show envs_dirs

清理缓存# 重新查看环境列表

conda clean --all  
conda env list    
pyinstaller --onefile --windowed  --add-data "E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\PyQt6\Qt6\bin;PyQt6\Qt6\bin"   --name="BrowserManager"   --clean ui2.py

--add-data 原路径:目标路径

如果某模块或者PyQt6的DLL文件没有被正确包含或加载。 使用PyInstaller自动收集PyQt6(推荐)

pyinstaller --onefile --windowed --name="BrowserManager" --clean --collect-all PyQt6 ui2.py

因为我当前的目录,是反编译pythonexe出来解包后的目录, 实际python环境中并没有安装pyqt6 但是在解包目录中应该是有的,所以可以正常运行,现在想重新打包

pyinstaller   --onefile   --windowed   --name="BrowserManager"   --clean   --paths="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted"  --add-data "E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\PyQt6\Qt6\bin;PyQt6\Qt6\bin"   --add-data "E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\PyQt6\Qt6\plugins;PyQt6\Qt6\plugins"     --hidden-import=PyQt6.QtWidgets   --hidden-import=PyQt6.QtCore   --hidden-import=PyQt6.QtGui  --exclude-module=tkinter   ui2.py
E:\pycharm\py312unpack\python.exe -m PyInstaller   -D   -w   --clean   --noconfirm   --name BrowserManager   --add-data "config.json;."   --hidden-import=PyQt6.sip   ui2.py

dir打包方法

dir
 驱动器 E 中的卷是 本地磁盘
 卷的序列号是 E07A-DBD3

 E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted 的目录

2025/12/26  17:29    <DIR>          .
2025/12/26  17:29    <DIR>          .. 
2025/12/16  15:28             3,833 api_client.py
2025/12/26  17:27            14,044 browser_ui.py
2025/12/16  15:20             1,206 captcha_recognition.py
2025/12/26  17:27               324 config.json
2025/12/17  09:33             2,720 http_api.py
2025/12/16  17:55    <DIR>          selenium
2025/12/26  16:33            55,814 start_operation.py
2025/12/26  16:44               610 ui2.py
2025/12/26  17:23    <DIR>          websocket
2025/12/26  17:27    <DIR>          __pycache__
python 解释器的环境目录是E:\pycharm\py312unpack
现在根据可运行目录代码,最大可运行打包出exe
这是主入口程序代码# -*- coding: gbk -*-
import datetime
import sys
from PyQt6.QtWidgets import QApplication
from browser_ui import BrowserManagerUI
def check_expiration():
    expire_date = datetime.date(2025, 12, 28)
    current_date = datetime.date.today()
    return current_date >= expire_date
if __name__ == '__main__':
    app = QApplication(sys.argv)
    if check_expiration():
        # 创建报错提示框
        driver = BrowserManagerUI().mini()
        print("")
        exit(-1)
    # ========================================
    window = BrowserManagerUI()
    window.show()
    sys.exit(app.exec())
pyinstaller   --onedir   --windowed   --name="BrowserManager"   --clean   --paths="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted"   --paths="E:\pycharm\py312unpack\Lib\site-packages"   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\api_client.py;."   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\browser_ui.py;."   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\captcha_recognition.py;."   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\config.json;."   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\http_api.py;."   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\start_operation.py;."   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\selenium;selenium"   --add-data="E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\websocket;websocket"   --hidden-import=PyQt6.QtWidgets   --hidden-import=PyQt6.QtCore   --hidden-import=PyQt6.QtGui   --hidden-import=api_client   --hidden-import=browser_ui   --hidden-import=captcha_recognition   --hidden-import=http_api   --hidden-import=start_operation   --hidden-import=selenium   --hidden-import=websocket E:\pythonProject\Homework\python-exe-unpacker\python-exe-unpacker-master\dy2.exe_extracted\PYZ-03.pyz_extracted\ui2.py

单文件(--onefile)模式下,PyInstaller 会把文件解压到临时目录,容易导致 Qt 的路径识别失败。改用目录模式打包,是解决 PyQt6 打包最稳定的方案

nuitka打包

 nuitka   --onefile   --windows-disable-console   --enable-plugin=pyqt6   --output-filename=BrowserManager.exe   ui2.py
 nuitka   --onefile   --windows-disable-console   --enable-plugin=pyqt6 --include-data-dir="E:/pycharm/py312unpack/Lib/site-packages/PyQt6/Qt6/plugins"="PyQt6/Qt6/plugins"   --output-filename=BrowserManager.exe   ui2.py

终极Nuitka编译命令(显示控制台+强制包含Qt插件+所有依赖)

python -m nuitka   --onefile   --windows-disable-console  --enable-plugin=pyqt6 --include-package=selenium  --include-package=websocket     --include-data-dir=selenium=./selenium   --include-data-dir=websocket=./websocket   --include-data-file=config.json=./   --include-qt-plugins=all   --follow-imports   --nofollow-import-to=tkinter  --output-filename=BrowserManager.exe   --output-dir=nuitka_build   --show-memory   --show-progress   ui2.py
nuitka   --standalone     --onefile     --mingw64     --lto=no     --assume-yes-for-downloads     --show-progress     --show-memory     --windows-console-mode=force   --enable-plugin=pyqt6     --include-package=selenium     --include-package=websocket     --include-package=api_client     --include-package=browser_ui     --include-package=captcha_recognition     --include-package=http_api     --include-package=start_operation   --include-data-file=config.json=config.json --include-package=selenium     --include-package=selenium.webdriver     --include-package=selenium.webdriver.common     --include-package=selenium.webdriver.chrome --include-package=selenium.webdriver.chromium   --include-package=selenium.webdriver.support   --include-module=selenium.webdriver.chromium.service --include-module=selenium.webdriver.common.service --include-module=selenium.webdriver.common.utils --include-package=selenium.webdriver.common --include-package=selenium.common.exceptions --include-module=selenium.webdriver.common.keys  --include-module=selenium.webdriver.chromium.webdriver   --include-module=selenium.webdriver.chromium.options   --include-module=selenium.webdriver.chromium.remote_connection --include-package=selenium.types --nofollow-import-to=tkinter     --nofollow-import-to=matplotlib     --nofollow-import-to=numpy     --nofollow-import-to=pandas     --nofollow-import-to=IPython     --nofollow-import-to=pytest     --windows-company-name="YourCompany"     --windows-product-name=broswermanager1    --windows-file-version=4     --windows-product-version=4    --windows-file-description="浏览器自动化管理工具"     --output-dir=dist     --output-filename=broswermanager.exe --show-memory   --show-progress   ui2.py

--include-package-to= 新版 Nuitka 的--include-package-to参数是专门解决递归包含问题的,比通配符更稳定。 若不想升级,用--include-package=selenium.* + --include-module=selenium.*替代,覆盖所有子模块。 新版才支持通配符

nuitka   --standalone     --onefile     --mingw64     --lto=no     --assume-yes-for-downloads     --show-progress     --show-memory     --windows-console-mode=force   --enable-plugin=pyqt6     --include-package=selenium     --include-package=websocket     --include-package=api_client     --include-package=browser_ui     --include-package=captcha_recognition     --include-package=http_api     --include-package=start_operation   --include-data-file=config.json=config.json --include-package=selenium     --include-package=selenium.webdriver     --include-package=selenium.webdriver.common     --include-package=selenium.webdriver.chrome --include-package=selenium.webdriver.chromium   --include-package=selenium.webdriver.support   --include-module=selenium.webdriver.chromium.service --include-module=selenium.webdriver.common.service --include-module=selenium.webdriver.common.utils --include-package=selenium.webdriver.common --include-package=selenium.common.exceptions --include-package=selenium.webdriver.common.desired_capabilities  --include-module=selenium.webdriver.common.keys --include-package=selenium.webdriver.common.options --include-package=selenium.webdriver.common.proxy --include-module=selenium.webdriver.chromium.webdriver   --include-module=selenium.webdriver.chromium.options   --include-module=selenium.webdriver.chromium.remote_connection --include-package=selenium.types --nofollow-import-to=tkinter     --nofollow-import-to=matplotlib     --nofollow-import-to=numpy     --nofollow-import-to=pandas     --nofollow-import-to=IPython     --nofollow-import-to=pytest     --windows-company-name="YourCompany"     --windows-product-name=broswermanager1    --windows-file-version=4     --windows-product-version=4    --windows-file-description="浏览器自动化管理工具"     --output-dir=dist     --output-filename=broswermanager.exe --show-memory   --show-progress   ui2.py

不加cmd窗口

nuitka   --standalone     --onefile     --mingw64     --lto=no     --assume-yes-for-downloads     --show-progress     --show-memory     --windows-console-mode=disable   --enable-plugin=pyqt6     --include-package=selenium     --include-package=websocket     --include-package=api_client     --include-package=browser_ui     --include-package=captcha_recognition     --include-package=http_api     --include-package=start_operation   --include-data-file=config.json=config.json --include-package=selenium     --include-package=selenium.webdriver     --include-package=selenium.webdriver.common     --include-package=selenium.webdriver.chrome --include-package=selenium.webdriver.chromium   --include-package=selenium.webdriver.support   --include-module=selenium.webdriver.chromium.service --include-module=selenium.webdriver.common.service --include-module=selenium.webdriver.common.utils --include-package=selenium.webdriver.common --include-package=selenium.common.exceptions --include-package=selenium.webdriver.common.desired_capabilities  --include-module=selenium.webdriver.common.keys --include-package=selenium.webdriver.common.options --include-package=selenium.webdriver.common.proxy --include-module=selenium.webdriver.chromium.webdriver   --include-module=selenium.webdriver.chromium.options   --include-module=selenium.webdriver.chromium.remote_connection --include-package=selenium.types --nofollow-import-to=tkinter     --nofollow-import-to=matplotlib     --nofollow-import-to=numpy     --nofollow-import-to=pandas     --nofollow-import-to=IPython     --nofollow-import-to=pytest     --windows-company-name="YourCompany"     --windows-product-name=broswermanager1    --windows-file-version=4     --windows-product-version=4    --windows-file-description="浏览器自动化管理工具"     --output-dir=dist     --output-filename=broswermanager.exe --show-memory   --show-progress   ui2.py