install_playwright

install-playwright-python

PyPI version CI

Execute playwright install from Python.

Install

pip install install-playwright

Example

from install_playwright import install
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    install([p.chromium])
    browser = p.chromium.launch()
    # ...
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        install([p.chromium])
        browser = await p.chromium.launch()
        # ...

Skip the install when the browser is already downloaded:

from install_playwright import install, is_installed
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    if not is_installed([p.chromium]):
        install([p.chromium])

License

MIT

  1""".. include:: ../README.md"""  # noqa: D415
  2
  3from __future__ import annotations
  4
  5import importlib.metadata
  6import subprocess
  7from os import EX_OK
  8from pathlib import Path
  9from typing import TYPE_CHECKING
 10
 11from playwright._impl._driver import compute_driver_executable, get_driver_env
 12
 13if TYPE_CHECKING:
 14    from playwright.async_api import BrowserType as AsyncBrowserType
 15    from playwright.sync_api import BrowserType as SyncBrowserType
 16
 17try:
 18    __version__ = importlib.metadata.version(__name__)
 19except importlib.metadata.PackageNotFoundError:
 20    __version__ = "0.0.0"
 21
 22
 23def is_installed(
 24    browser_types: list[SyncBrowserType] | list[AsyncBrowserType],
 25) -> bool:
 26    """Check whether the browsers are already downloaded locally.
 27
 28    Args:
 29        browser_types (list[SyncBrowserType] | list[AsyncBrowserType]): List of `BrowserType` objects.
 30
 31    Returns:
 32        bool: `True` if every given browser is already installed
 33    """
 34    return all(bool(bt.executable_path) and Path(bt.executable_path).exists() for bt in browser_types)
 35
 36
 37def install(
 38    browser_types: list[SyncBrowserType] | list[AsyncBrowserType],
 39    *,
 40    with_deps: bool = False,
 41    only_shell: bool = False,
 42    no_shell: bool = False,
 43    force: bool = False,
 44) -> bool:
 45    """Install playwright and deps if needed.
 46
 47    Args:
 48        browser_types (list[SyncBrowserType] | list[AsyncBrowserType]): List of `BrowserType` objects.
 49        with_deps (bool, optional): install with dependencies. Defaults to `False`.
 50        only_shell (bool, optional): install only browser shell. Defaults to `False`.
 51        no_shell (bool, optional): do not install browser shell. Defaults to `False`.
 52        force (bool, optional): force re-installation. Defaults to `False`.
 53
 54    Returns:
 55        bool: succeeded or failed
 56    """
 57    driver_executable, driver_cli = compute_driver_executable()
 58    args: list[str] = [driver_executable, driver_cli, "install"] + [bt.name for bt in browser_types]
 59
 60    if with_deps:
 61        args.append("--with-deps")
 62
 63    if only_shell and no_shell:
 64        msg = "`only_shell` and `no_shell` cannot be both `True`"
 65        raise ValueError(msg)
 66
 67    if only_shell:
 68        args.append("--only-shell")
 69
 70    if no_shell:
 71        args.append("--no-shell")
 72
 73    if force:
 74        args.append("--force")
 75
 76    proc = subprocess.run(  # noqa: S603
 77        args,
 78        env=get_driver_env(),
 79        capture_output=True,
 80        text=True,
 81        check=False,
 82    )
 83
 84    return proc.returncode == EX_OK
 85
 86
 87def uninstall(
 88    *,
 89    all_browsers: bool = False,
 90) -> bool:
 91    """Uninstall playwright browsers.
 92
 93    Args:
 94        all_browsers (bool, optional): uninstall all browsers. Defaults to `False`.
 95
 96    Returns:
 97        bool: succeeded or failed
 98    """
 99    driver_executable, driver_cli = compute_driver_executable()
100    args = [driver_executable, driver_cli, "uninstall"]
101
102    if all_browsers:
103        args.append("--all")
104
105    proc = subprocess.run(  # noqa: S603
106        args,
107        env=get_driver_env(),
108        capture_output=True,
109        text=True,
110        check=False,
111    )
112
113    return proc.returncode == EX_OK
114
115
116__all__ = ("install", "is_installed", "uninstall")
def install( browser_types: list[playwright.sync_api._generated.BrowserType] | list[playwright.async_api._generated.BrowserType], *, with_deps: bool = False, only_shell: bool = False, no_shell: bool = False, force: bool = False) -> bool:
38def install(
39    browser_types: list[SyncBrowserType] | list[AsyncBrowserType],
40    *,
41    with_deps: bool = False,
42    only_shell: bool = False,
43    no_shell: bool = False,
44    force: bool = False,
45) -> bool:
46    """Install playwright and deps if needed.
47
48    Args:
49        browser_types (list[SyncBrowserType] | list[AsyncBrowserType]): List of `BrowserType` objects.
50        with_deps (bool, optional): install with dependencies. Defaults to `False`.
51        only_shell (bool, optional): install only browser shell. Defaults to `False`.
52        no_shell (bool, optional): do not install browser shell. Defaults to `False`.
53        force (bool, optional): force re-installation. Defaults to `False`.
54
55    Returns:
56        bool: succeeded or failed
57    """
58    driver_executable, driver_cli = compute_driver_executable()
59    args: list[str] = [driver_executable, driver_cli, "install"] + [bt.name for bt in browser_types]
60
61    if with_deps:
62        args.append("--with-deps")
63
64    if only_shell and no_shell:
65        msg = "`only_shell` and `no_shell` cannot be both `True`"
66        raise ValueError(msg)
67
68    if only_shell:
69        args.append("--only-shell")
70
71    if no_shell:
72        args.append("--no-shell")
73
74    if force:
75        args.append("--force")
76
77    proc = subprocess.run(  # noqa: S603
78        args,
79        env=get_driver_env(),
80        capture_output=True,
81        text=True,
82        check=False,
83    )
84
85    return proc.returncode == EX_OK

Install playwright and deps if needed.

Arguments:
  • browser_types (list[SyncBrowserType] | list[AsyncBrowserType]): List of BrowserType objects.
  • with_deps (bool, optional): install with dependencies. Defaults to False.
  • only_shell (bool, optional): install only browser shell. Defaults to False.
  • no_shell (bool, optional): do not install browser shell. Defaults to False.
  • force (bool, optional): force re-installation. Defaults to False.
Returns:

bool: succeeded or failed

def is_installed( browser_types: list[playwright.sync_api._generated.BrowserType] | list[playwright.async_api._generated.BrowserType]) -> bool:
24def is_installed(
25    browser_types: list[SyncBrowserType] | list[AsyncBrowserType],
26) -> bool:
27    """Check whether the browsers are already downloaded locally.
28
29    Args:
30        browser_types (list[SyncBrowserType] | list[AsyncBrowserType]): List of `BrowserType` objects.
31
32    Returns:
33        bool: `True` if every given browser is already installed
34    """
35    return all(bool(bt.executable_path) and Path(bt.executable_path).exists() for bt in browser_types)

Check whether the browsers are already downloaded locally.

Arguments:
  • browser_types (list[SyncBrowserType] | list[AsyncBrowserType]): List of BrowserType objects.
Returns:

bool: True if every given browser is already installed

def uninstall(*, all_browsers: bool = False) -> bool:
 88def uninstall(
 89    *,
 90    all_browsers: bool = False,
 91) -> bool:
 92    """Uninstall playwright browsers.
 93
 94    Args:
 95        all_browsers (bool, optional): uninstall all browsers. Defaults to `False`.
 96
 97    Returns:
 98        bool: succeeded or failed
 99    """
100    driver_executable, driver_cli = compute_driver_executable()
101    args = [driver_executable, driver_cli, "uninstall"]
102
103    if all_browsers:
104        args.append("--all")
105
106    proc = subprocess.run(  # noqa: S603
107        args,
108        env=get_driver_env(),
109        capture_output=True,
110        text=True,
111        check=False,
112    )
113
114    return proc.returncode == EX_OK

Uninstall playwright browsers.

Arguments:
  • all_browsers (bool, optional): uninstall all browsers. Defaults to False.
Returns:

bool: succeeded or failed