前置条件
没有安装自带环境,则
playwright install
安装了自带的浏览器
查看playwright的浏览器的位置
playwright install --dry-run
打开此文件夹可以看到
新建一个多层级目录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。