Using Playwright on PythonAnywhere

NOTE These instructions are for the system image fishnchips, glastonbury or haggis. If you have an older system image, you will need to update your system image to use these instructions. Also note that Playwright will only work in tasks, web apps and consoles. It will also work in notebooks, but only on haggis. It will not work over SSH.

If you have a paid account on PythonAnywhere, you can use Playwright to access other sites using a headless browser -- for example, for scraping. Installing it is a little different to how you might do so locally.

You'll need to install it into a virtualenv, because it's not compatible with some of our pre-installed packages. So, for example:

mkvirtualenv my-playwright-env --python=python3.10
pip install playwright

It will take up about 130MiB disk when installed (perhaps using a bit more than that during the install due to its temporary files).

The next step would normally to install the browsers using playwright install. However, that won't work on PythonAnywhere -- but we do already have Chromium installed, so you can use that by adding some extra config to your code. Here's a minimal example:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        args=["--disable-gpu", "--no-sandbox", "--headless"]
    page = browser.new_page()

Many thanks to hcaptcha in our forums for working out what the the trick was to make it work!