Install Playwright Browsers from Python
install-playwright is a Python library that provides a programmatic way to execute the `playwright install` CLI command, which downloads browser binaries (Chromium, Firefox, WebKit) required by the Playwright automation library. This allows Python applications to manage Playwright browser installations directly, without needing to shell out to a subprocess. The current version is 1.0.0, and it follows an infrequent release cadence, primarily updating for compatibility or minor feature additions.
Common errors
-
NameError: name 'p' is not defined
cause The `p` object (representing the Playwright context) was not initialized or is out of scope when calling `install_playwright.install([p.chromium])`.fixWrap your `install` call within a `with sync_playwright() as p:` block or ensure `p` is available from an `async_playwright` context. -
ModuleNotFoundError: No module named 'playwright'
cause The main `playwright` Python library has not been installed, which is a prerequisite for `install-playwright` and for providing the `BrowserType` objects.fixRun `pip install playwright` before installing `install-playwright` or attempting to use it. -
TypeError: 'BrowserType' object is not iterable
cause The `install` function expects a list of `BrowserType` objects, but a single `BrowserType` object was passed directly without being wrapped in a list.fixEnsure you pass a list to the `install` function, even if it contains only one browser, e.g., `install([p.chromium])` instead of `install(p.chromium)`.
Warnings
- gotcha The `install-playwright` library only handles the *installation of browser binaries* (Chromium, Firefox, WebKit). It does NOT install the `playwright` Python package itself. You must `pip install playwright` separately.
- gotcha The `install` function expects `BrowserType` objects from the `playwright` library (e.g., `p.chromium`). You must initialize `playwright.sync_api.sync_playwright` or `playwright.async_api.async_playwright` to obtain these objects before calling `install_playwright.install`.
- gotcha By default, `install_playwright.install()` will install all supported browsers if called without arguments, similar to `playwright install`. To install specific browsers, you must pass a list of `BrowserType` objects.
Install
-
pip install install-playwright -
pip install playwright
Imports
- install
from install_playwright import install
- sync_playwright
from playwright.sync_api import sync_playwright
Quickstart
from playwright.sync_api import sync_playwright
from install_playwright import install
with sync_playwright() as p:
# Install Chromium browser binaries
print("Installing Chromium...")
install([p.chromium])
print("Chromium installed.")
# Optionally, install other browsers
# print("Installing Firefox and WebKit...")
# install([p.firefox, p.webkit])
# print("Firefox and WebKit installed.")
# Example of using Playwright after installation
browser = p.chromium.launch()
page = browser.new_page()
page.goto("http://example.com")
print(f"Page title: {page.title()}")
browser.close()