Deploying playwright-python on Heroku
Asked Answered
M

4

6

I am using a python module that uses playwright in my heroku flask app. The installation instructions for the module require that I install the browser binaries like:

python -m playwright install

While when I deploy it locally it works, I seem unable to incorporate the browser binary installation in the deployment. I have tried to use the heroku playwright buildpack instead (https://github.com/mxschmitt/heroku-playwright-buildpack), but this doesn't seem to work, and I get an error like:

2020-11-17T23:06:42.252585+00:00 app[web.1]: "webkit" browser was not found.
2020-11-17T23:06:42.252585+00:00 app[web.1]: Please complete Playwright installation via running
2020-11-17T23:06:42.252585+00:00 app[web.1]: 
2020-11-17T23:06:42.252586+00:00 app[web.1]:     "python -m playwright install"

I also tried manually adding the python -m playwright install command in a buildpack, but this does not work either. Is there any way to install the binaries properly using playwright in heroku?

Mantelpiece answered 17/11, 2020 at 23:21 Comment(0)
A
2

What worked for me as a workaround, and I am 100% sure it is not the right solution is to add this to my python code:

from subprocess import Popen, PIPE

p = Popen([sys.executable, "-m", "playwright", "install"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
#output, err = p.communicate()
#print(output)
Aplasia answered 27/8, 2021 at 9:30 Comment(0)
P
1

Heroku doesn't support webkit at the time, with other browsers, you can use this buildpack: https://github.com/mxschmitt/heroku-playwright-buildpack/

Priory answered 6/1, 2021 at 23:39 Comment(1)
doesn't help for me if adding it actually.Hungarian
A
0

I made a buildpack to install Playwright browsers on Heroku.

There are instructions in the README on how to use it.

Disclaimer: I made this for the sole purpose of using Chromium but the code can install webkit and firefox too. Run-time behaviour with non-Chromium browsers is unknown since I don't use them.

Alina answered 7/2 at 22:22 Comment(0)
T
0

Here is a solution demonstrating how to deploy playwright properly on heroku and make it work:

  1. add playwright to your requirements.txt

  2. Add the Heroku Playwright Buildpack right after heroku/python [this will download the apt files]

  3. If you only need one browser, specify it in your Config VAR as PLAYWRIGHT_BUILDPACK_BROWSERS [e.g. chromium]

  4. As mentioned above, install playwright just before launching the browser

    async def intercept_responses(url, cookies, headers):
     command = [sys.executable, "-m", "playwright", "install", "chromium"]
     process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     stdout, stderr = process.communicate()
    
     if process.returncode != 0:
         print(f"Error installing Playwright:\n{stderr.decode('utf-8')}")
     else:
         print(f"Playwright installation successful:\n{stdout.decode('utf-8')}")
    
     async with async_playwright() as p:
         browser = await p.chromium.launch(headless=True)
         context = await browser.new_context()
Tetryl answered 30/6 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.