Tony
Tony

Reputation: 61

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

Upvotes: 6

Views: 17891

Answers (3)

Michael Hauptvogel
Michael Hauptvogel

Reputation: 131

I hade the same issue, this helped:

  1. sudo npm install -g playwright
  2. sudo pip install playwright
  3. playwright install

Upvotes: 0

osman
osman

Reputation: 151

mxschmitt said on GitHub:

the problem might be that your file is named playwright.py, maybe try renaming it to trying_playwright.py instead.

Upvotes: 15

Natan Budny
Natan Budny

Reputation: 81

Referencing this:

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

  5. playwright install
    

Upvotes: 5

Related Questions