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

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

时间:2023-11-24 15:34:02浏览次数:34  
标签:playwright Pyinstaller zip python win64 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了

 

标签:playwright,Pyinstaller,zip,python,win64,Download,net,chromium
From: https://www.cnblogs.com/Im-Victor/p/17853854.html

相关文章

  • python可视化打包exe
    安装Auto-py-to-exePython环境要大于或等于2.7然后在cmd里面输入pipinstallauto-py-to-exe安装完成之后,我们就可以在cmd输入一下命令启动auto-py-to-exe启动之后可以切换语言为中文,剩下的就看情况操作了......
  • PYTHON实现EXCEL数据导入MYSQL
    #coding=utf8importpymysqlimportosimportpandasaspdhost='127.0.0.1'port=3308user='root'password='*****'db='impairment_testing'conn=pymysql.connect(host=host,port=port,user=user,password=password,db=db......
  • python用playwright自动化测试程序打包exe
    playwright自动化测试代码写好后,打包为exe运行在目标PC上可能出现错误。原因:1、运行的PC没有响应的浏览器。2、playwright没有打包到代码中。所以本例用AutoPytoExe为例来制作exe程序解决问题。1、安装: 2、安装完成之后,我们就可以输入:auto-py-to-exe,来启动auto-py-to-exe......
  • python中的虚拟环境
    虚拟环境介绍:虚拟环境是一种在项目级别隔离Python依赖的方法。通过创建虚拟环境,你可以为每个项目设置独立的Python环境,从而解决全局安装可能导致的问题。虚拟环境可以包含自己的Python解释器和依赖库,与其他虚拟环境和系统环境隔离开。虚拟环境使用场景:项目隔离:当您需要......
  • Python常见文件读写方法有哪些?
    在Python中,文件读写是非常常见的操作之一,因此提供了多种文件读写模式以及文件读写方法。那么Python常见文件读写方法有哪些?具体请看下文。文件读写模式在Python中,文件读写模式是指打开文件时使用的模式。Python提供了多种文件读写模式,包括:①读模式("r"):以只读方式......
  • Python 数据类型
    Task2数据类型常用内置类型基本的数据类型整数Integer(int)浮点数Float布尔值Boolean(bool)类型Type坦白来说,type是一种面向类的对象,python是一种面向的对象友好的语言print(type(2)) #int型print(type(2.3)) #float型的print(type(2>2.3)) #bool型......
  • Python 变量与函数
    Task3变量与函数变量变量是一个名字,他所指的是一段数据使用=来对这段区域进行复制x=5print(x)print(x*2)新的值会覆盖旧的值新的值的数据类型不必与旧的值的数据类型相同y=10print(y-2)y=Trueprint(y)运行结果:变量命名规则:必须以字母或者下划......
  • python 题目:数字比较。
    #!/usr/bin/python#-*-coding:UTF-8-*- if __name__ == '__main__':  i = 10  j = 20   if i > j:     print ('%d大于%d' % (i,j))   elif i == j:     print ('%d等于%d' % (i,j))   elif i < j:   ......
  • 代码随想训练营第四十二天(Python)| 0-1 背包基础、416. 分割等和子集
    [背包基础]题目:有n件物品和一个最多能背重量为w的背包。第i件物品的重量是weight[i],得到的价值是value[i]。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。1、二维方式解决背包问题classSolution:defsolve_bag(self,weight,value,bag_weight):......
  • Python + BeautifulSoup 采集
    Python是一种非常流行的编程语言,也是开发网络爬虫和数据采集工具的首选语言。在Python中,有许多第三方库可以用于网络爬虫和数据采集,比如requests、beautifulsoup4、selenium等。下面是一个简单的例子,使用requests库采集一个网页:importrequests#发送GET请求response=......