playwright-community / heroku-playwright-buildpack

Buildpack for running Playwright with Chromium and Firefox on Heroku.
https://heroku.playwright.tech
46 stars 43 forks source link

Playwright fails to launch Chromium on Heroku despite executable being present #26

Open justinsane opened 9 months ago

justinsane commented 9 months ago

Description

I'm facing an issue while deploying a Python app on Heroku that utilizes Playwright to launch Chromium. Despite following best practices and ensuring the Chromium executable is present in the expected directory, I consistently receive an error stating that the "executable doesn't exist".

Env Details Platform: Heroku Buildpacks (in order): mxschmitt/heroku-playwright-buildpack heroku/python Python V: 3.11 Playwright V: 1.36.0 Heroku Stack: heroku-22 Relevant Code:

with sync_playwright() as p:
  # Troubleshooting for local and Heroku build
  default_path = "/app/.heroku/python/lib/python3.11/site-packages/playwright/driver/package/.local-browsers/chromium-1071/chrome-linux/chrome"
  chromium_path = os.getenv("PLAYWRIGHT_CHROMIUM_PATH", default_path)

  browser = p.chromium.launch(
    headless=True, # Set False for debugging and local dev
    args=['--no-sandbox'],
    executable_path=chromium_path
      )
   # ... rest of code

Error Message playwright._impl._api_types.Error: Failed to launch chromium because executable doesn't exist at /app/.heroku/python/lib/python3.11/site-packages/playwright/driver/package/.local-browsers/chromium-1071/chrome-linux/chrome

However, running heroku run bash and verify presence of the executable using ls, I can clearly see the Chromium executable present at the mentioned path.

Steps Taken:

Verified the presence of the Chromium executable on the Heroku Dyno.

Ensured the Playwright buildpack is correctly set up and is first in the order.

Tried using different environment variables including PLAYWRIGHT_BROWSERS_PATH and PLAYWRIGHT_CHROMIUM_PATH.

Checked for any potential conflicts with Selenium (previously used in the project) and its associated environment variables.

Expected Playwright should successfully launch Chromium without any errors.

Actual Playwright consistently fails to launch Chromium with an error stating the executable doesn't exist, even though it's present in the directory.

justinsane commented 9 months ago

This solution on Stack Overflow helped me - Stack Overflow

Heroku's filesystem is both ephemeral and dyno-local. Any changes made to it are lost when the dyno restarts. More importantly at the moment, when you run something like heroku run bash you don't connect to a running dyno. Instead, you get a separate one-off dyno.

Installing Chromium (or making any other filesystem changes) in such an environment will never affect what is available in your other dynos.

It looks like Playwright really wants to manage its own browser binaries. Your best bet might be to run playwright install near the beginning of your script, e.g. something like this:

import subprocess
from playwright.sync_api import sync_playwright

subprocess.run(["playwright", "install"])

with sync_playwright() as playwright:
    # ...

This should make sure that the required binaries are always available at runtime.

Thomas-Boi commented 4 months ago

I recently made a buildpack to address this issue. I did made it to use Chromium but it should work with Firefox and Webkit as well. Feel free to give it a look