You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将使用SeleniumBase的网页操作录制Python项目打包为客户端无依赖exe

可行打包方案(基于PyInstaller调整配置实现)

你之前PyInstaller打包失败的核心原因是SeleniumBase存在大量隐性依赖:包括内置的浏览器驱动、配置文件、JS脚本资源,以及动态导入的模块,默认打包规则不会自动收录这些内容,按照以下步骤调整即可:


步骤1:确认运行环境与安装PyInstaller

首先在开发环境验证项目可以正常启动,无缺包问题,然后在项目根目录执行以下命令安装兼容性适配的PyInstaller版本:
pip install pyinstaller==4.10


步骤2:编写自定义打包spec配置文件

在项目根目录新建build.spec,内容如下,程序会自动识别SeleniumBase安装路径,无需手动修改:

# -*- mode: python ; coding: utf-8 -*-
import sys
import os
# 自动获取seleniumbase的安装路径
site_packages = [p for p in sys.path if 'site-packages' in p][0]
seleniumbase_path = os.path.join(site_packages, 'seleniumbase')

block_cipher = None

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    # 收录所有动态导入的隐藏模块
    hiddenimports=[
        'seleniumbase',
        'seleniumbase.__main__',
        'seleniumbase.core',
        'seleniumbase.fixtures',
        'seleniumbase.plugins',
        'seleniumbase.utilities',
        'seleniumbase.driver',
        'tqdm',
        'validators',
        'Tinkerer'
    ],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
# 收录所有需要打包的资源文件
datas = [
    (seleniumbase_path, 'seleniumbase'),
    ('web_actions_recorder', 'web_actions_recorder'),
    ('logo.ico', '.')
]
a.datas += datas

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='网页操作录制工具',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True, # 不需要启动黑框可改为False
    disable_windowed_traceback=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon='logo.ico'
)

步骤3:执行打包

在项目根目录下执行命令:
pyinstaller build.spec
打包完成后,可直接运行的exe文件存放在dist目录下,客户端无需安装任何依赖即可启动。


兜底备选方案

如果上述方案仍有问题,可采用便携Python打包思路:

  • 下载Python3.7.9官方便携版,在便携环境内安装所有项目依赖
  • 编写启动bat脚本,内容为python main.py
  • 将便携Python包、项目代码、启动脚本一起打包为自解压压缩包,设置解压后自动运行启动脚本即可。

内容的提问来源于stack exchange,提问作者ElSheikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 09:45:04