首页 > 编程语言 >python+playwright 学习-43 Pyinstaller打包生成独立的可执行文件。

python+playwright 学习-43 Pyinstaller打包生成独立的可执行文件。

时间:2023-03-30 23:15:04浏览次数:42  
标签:playwright Pyinstaller python win64 https Download net chromium

前言

playwright 与Pyinstaller结合使用来创建独立的可执行文件。

本地化安装

有同学提到说想打成一个exe的独立包,但是执行playwright install会默认把 chromium,firefox 和 webkit 三个浏览器安装到系统目录。
这样打包的时候就找不到启动的浏览器文件。于是就想到把浏览器文件下载到我们代码的项目目录,打到一起。

在playwright 官方文档中有提到相关资料:
您可以将 Playwright 与Pyinstaller结合使用来创建独立的可执行文件。

# main.py
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("http://whatsmyuseragent.org/")
    page.screenshot(path="example.png")
    browser.close()

如果你想将浏览器与可执行文件捆绑在一起:

set PLAYWRIGHT_BROWSERS_PATH=0
playwright install chromium
pyinstaller -F main.py

将浏览器与可执行文件捆绑在一起将生成更大的二进制文件。建议只捆绑您使用的浏览器。

上面这段就是官方文档提到的,资料比较少,接下来详细讲解下set PLAYWRIGHT_BROWSERS_PATH=0 的作用

chromium 本地化安装

我们可以先创建一个虚拟环境

在虚拟环境下,安装你需要的包

pip install playwright

先设置环境变量

set PLAYWRIGHT_BROWSERS_PATH=0

设置完成后使用playwright install --dry-run查看浏览器安装路径

(venv) D:\demo\untitled1>playwright install --dry-run
browser: chromium version 112.0.5615.29
  Install location:    d:\demo\untitled1\venv\Lib\site-packages\playwright\driver\package\.local-browsers\chromium-1055
  Download url:        https://playwright.azureedge.net/builds/chromium/1055/chromium-win64.zip
  Download fallback 1: https://playwright-akamai.azureedge.net/builds/chromium/1055/chromium-win64.zip
  Download fallback 2: https://playwright-verizon.azureedge.net/builds/chromium/1055/chromium-win64.zip

browser: firefox version 111.0
  Install location:    d:\demo\untitled1\venv\Lib\site-packages\playwright\driver\package\.local-browsers\firefox-1391
  Download url:        https://playwright.azureedge.net/builds/firefox/1391/firefox-win64.zip
  Download fallback 1: https://playwright-akamai.azureedge.net/builds/firefox/1391/firefox-win64.zip
  Download fallback 2: https://playwright-verizon.azureedge.net/builds/firefox/1391/firefox-win64.zip

browser: webkit version 16.4
  Install location:    d:\demo\untitled1\venv\Lib\site-packages\playwright\driver\package\.local-browsers\webkit-1811
  Download url:        https://playwright.azureedge.net/builds/webkit/1811/webkit-win64.zip
  Download fallback 1: https://playwright-akamai.azureedge.net/builds/webkit/1811/webkit-win64.zip
  Download fallback 2: https://playwright-verizon.azureedge.net/builds/webkit/1811/webkit-win64.zip

browser: ffmpeg
  Install location:    d:\demo\untitled1\venv\Lib\site-packages\playwright\driver\package\.local-browsers\ffmpeg-1008
  Download url:        https://playwright.azureedge.net/builds/ffmpeg/1008/ffmpeg-win64.zip
  Download fallback 1: https://playwright-akamai.azureedge.net/builds/ffmpeg/1008/ffmpeg-win64.zip
  Download fallback 2: https://playwright-verizon.azureedge.net/builds/ffmpeg/1008/ffmpeg-win64.zip

如果你只用一个浏览器,那么你就只下载一个,以chromium 为例,执行

playwright install chromium

于是你可以在此目录找到d:\demo\untitled1\venv\Lib\site-packages\playwright\driver\package\.local-browsers\webkit-1811

顺着路径,你就会找到chromium-1055 和 ffmpeg 这2个文件。

需注意的是chromium-1055后面的1055 是版本号,此数字不能随便改,让它自己下载匹配的版本,不能直接复制别人下载的包。
并且安装包的位置不能随便乱放,一定要是先设置环境变量PLAYWRIGHT_BROWSERS_PATH=0, 再去安装让它自己去下载后放到指定的目录

Pyinstaller打包

安装Pyinstaller

pip install pyinstaller

安装完成后执行打包

pyinstaller -F main.py

icon 制作

-i参数打包的时候可以自定义icon图标

-i <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">, --icon <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">
                        FILE.ico: apply that icon to a Windows executable.
                        FILE.exe,ID, extract the icon with ID from an exe.
                        FILE.icns: apply the icon to the .app bundle on Mac OS
                        X. Use "NONE" to not apply any icon, thereby making
                        the OS to show some default (default: apply
                        PyInstaller's icon)

先找一张icon图片放到项目跟目录(注意并不是每个图片格式都可以,必须是icon格式)

-i 参数打包

pyinstaller -F yoyoblog.py -i favicon.ico

打包完成重新双击运行,会看到左上角有自己的icon了

icon在线制作https://www.bitbug.net/

标签:playwright,Pyinstaller,python,win64,https,Download,net,chromium
From: https://www.cnblogs.com/yoyoketang/p/17274719.html

相关文章

  • Python魔力方法
    Python的魔术方法(MagicMethods)也称为双下划线方法(doubleunderscoremethod),以双下划线开头和结尾,用于重载类的特殊行为。可以使类的实例对象表现出像内置类型的行为,如加、减、乘、切片、比较等,增加代码的可读性和可维护性。以下是Python中一些重要的魔术方法:1.__init__方法__in......
  • python基础学习总结
    python关键字也是以下划线或者字母开头。python关键字可以通过导包获取关键字如下: ['False','None','True','and','as','assert','async','await','break','class','continue'......
  • [Python]异步回调函数
    importasynciofromfunctoolsimportpartialfromasyncioimportFutureasyncdeff1():print(1)awaitasyncio.sleep(2)print(2)return"f1"defcallback1(future:Future):print(future.result())print("我是f1的回调函数&......
  • Python 3 vs Python 2 All In One
    Python3vsPython2AllInOnePython3.x与Python2.x版本区别https://www.runoob.com/python/python-2x-3x.htmlhttps://www.datacamp.com/blog/python-2-vs-3-everything-you-need-to-knowprintvsprint()如果Python2.x版本想使用使用Python3.x的print函......
  • 软件测试|web自动化测试神器playwright教程(八)
    前言selenium中提供了一个seleniumIDE的工具用于脚本录制,我们通过插件市场安装之后,便可以将我们对浏览器页面的操作录制成脚本,并输出成java或Python等语言的脚本,我们可以通过生成的脚本再次回放我们的操作。作为一个比selenium更加强大的web自动化测试工具,当然也拥有录制的功能了,......
  • Python 脚本接收命令行参数的多种方式 All In One
    Python脚本接收命令行参数的多种方式AllInOnesysargparseshellscripttensorflow...sys#!/usr/bin/envpython3#coding:utf8importsysargs=sys.argvprint("argslength:",len(args))print("argstype:",type(args))print("fu......
  • 跟着查老四学Python Day 3:数据结构与函数
    老猫:请您扮演一个python讲师,帮助一个没有代码经验的人自学python,以下是此前你设置的学习计划制定学习计划:将学习过程分为四个阶段,每个阶段关注不同的内容。第一周:掌握Python基础语法、数据类型、控制结构等。同时,学会如何在本地搭建Python开发环境。第二周:学习函数、模块、文件操......
  • Python计算机视觉基础实验3-显著性检测(HC&FC)
    一、实验基础图像显著性检测图像的显著性是指对于给定一副真实世界中的场景图像,人们会自动地识别出感兴趣区域,忽略掉不感兴趣的区域,即将注意力集中在图像中某些显著的部分区域。图像的注意预测,也称视觉显著性检测,指通过智能算法模拟人的视觉系统特点,预测人类的视觉凝视点(就是全神贯......
  • opencv-python 4.8. 图像金字塔
    理论通常,我们曾经使用恒定大小的图像。但在某些情况下,我们需要使用不同分辨率的(相同)图像。例如,在搜索图像中的某些内容时,如脸部,我们不确定该对象在所述图像中的大小。在这种情况下,我们需要创建一组具有不同分辨率的相同图像,并在所有图像中搜索对象。这些具有不同分辨率的图像被称......
  • [Python]异步wait和gather
    相同点:从功能上看,asyncio.wait和asyncio.gather实现的效果是相同的。不同点:使用方式不一样,wait需要传一个可迭代对象,gather传多个元素wait比gather更底层,gather可以直接得到结果,wait先得到future再得到结果wait可以通过设置timeout和return_when来终止任务gather可以......