pyinstaller带浏览器一起打包playwright 独立运行exe

前置条件

没有安装自带环境,则

playwright install

安装了自带的浏览器

查看playwright的浏览器的位置

playwright install --dry-run

image-20240730222139491

打开此文件夹可以看到

新建一个多层级目录playwright\driver\package.local-browsers

然后复制chromium-1124到playwright\driver\package.local-browsers

最后在pyinstaller的打包参数上加上chromium-1124的绝对路径

以下为示例

import shutil
import subprocess
import time

command = [
    "pyinstaller",
    "-w",
    "-n", "automation_tool",
    "-F", "main.py",
    "--add-data", r"D:\WorkSpace\SelfTools\playwright;playwright/",
    "--distpath=D:\\WorkSpace\\SelfTools"
]

command = " ".join(command)
print(command)


# 打包使用的虚拟环境
def main():
    """
    打包工具
    """
    subprocess.run(command)

    time.sleep(2)
    shutil.rmtree('./build')


if __name__ == '__main__':
    main()

–add-data D:/WorkSpace/SelfTools/playwright;playwright/

最后你生成的exe将会很大,我打包的大约320MB。

作者:Franciz小测测

物联沃分享整理
物联沃-IOTWORD物联网 » pyinstaller带浏览器一起打包playwright 独立运行exe

发表回复