'Can't import name 'sync_playwright' from 'playwright.sync_api' on MacOS and ubutunOS

I'm trying import playwright framework. However, I faced an issue as below:

Traceback (most recent call last):
  File "simple.py", line 1, in <module>
    from playwright.sync_api import sync_playwright
ImportError: cannot import name 'sync_playwright' from 'playwright.sync_api' (/home/acid/dev/play/venv
/lib/python3.8/site-packages/playwright/sync_api.py)

I've installed the playwright by command below:

pip install playwright==1.8.0a1
playwright install

Then, run script:

from playwright.sync_api import sync_playwright

    with sync_playwright() as p:
        for browser_type in [p.chromium, p.firefox, p.webkit]:
            browser = browser_type.launch()
            page = browser.new_page()
            page.goto('http://whatsmyuseragent.org/')
            page.screenshot(path=f'example-{browser_type.name}.png')
            browser.close()

However, it still didn't work. Do you have any suggest for this issue? Thank in advances



Solution 1:[1]

Referencing this:

  1. pip install --upgrade pip
    
  2. pip uninstall playwright
    
  3. pip install playwright
    
  4. Change to your script directory

  5. playwright install
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike