microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.51k stars 876 forks source link

Cannot get playwright example to work on macOS BigSur (Intel CPU) #453

Closed DaBrondo closed 3 years ago

DaBrondo commented 3 years ago

I receive this error when running the sync example code:

ImportError: cannot import name 'sync_playwright' from 'playwright.sync_api'

I've used venv to create a Python3 virtual environment and playwright installed via pip into that environment.

basic-ph commented 3 years ago

First time with playwright for me and I've received the same error:

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'm using Ubuntu 20.04 through WSL (version 2) and I installed playwright inside a virtualenv using:

$ pip install playwright
$ python -m playwright install
pavelfeldman commented 3 years ago

Please make sure you are using playwright==1.8.0a1 since the docs were all updated to match our v1 offering.

pavelfeldman commented 3 years ago

I'll make sure our samples for installation suggest that explicitly. Sorry for inconvenience!

basic-ph commented 3 years ago

Thank you for help @pavelfeldman May be useful to tell explicitly also that the are also some dependencies not installed using:

$ pip install playwright
$ python -m playwright install

I don't know if this is a situation related only to WSL or not but I got this message:

playwright._impl._api_types.Error: Host system is missing dependencies!

Install missing packages with:
sudo apt-get install libnss3\
  libnspr4\
  libatk1.0-0\
  libatk-bridge2.0-0\
  libcups2\
  libxkbcommon0\
  libgbm1\
  libgtk-3-0\
  libpango-1.0-0\
  libcairo2\
  libgdk-pixbuf2.0-0\
  libatspi2.0-0

PS: I think the suggested command (apt-get install ...) needs a whitespace after each package name to work. (see below) -- edit (I got also this):

playwright._impl._api_types.Error: Host system is missing dependencies!

  Install missing packages with:
      sudo apt-get install libegl1 \
          libnotify4 \
          libvpx6 \
          libopus0 \
          libwoff1 \
          libharfbuzz-icu0 \
          libgstreamer-plugins-base1.0-0 \
          libgstreamer-gl1.0-0 \
          libgstreamer-plugins-bad1.0-0 \
          libopenjp2-7 \
          libwebpdemux2 \
          libenchant1c2a \
          libsecret-1-0 \
          libhyphen0 \
          libgles2 \
          gstreamer1.0-libav
pavelfeldman commented 3 years ago

It looks like the original issue has been resolved and a new one filed for dependencies. Closing.

AnaKuzina commented 3 years ago

Hi, I'm unsuccessfully trying to use playwright. I've got this error: ImportError: cannot import name 'sync_playwright' from 'playwright' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/playwright/__init__.py)

though I've installed playwright using this command: pip install playwright==1.8.0a1

Please help me solve the issue. Thanks!

Matoo125 commented 3 years ago

I have the same issue. When I run

from playwright import sync_playwright

I'll get

ImportError: cannot import name 'sync_playwright' from 'playwright' (/home/matej/.pyenv/versions/myenv/lib/python3.8/site-packages/playwright/__init__.py)

also here is the content of __init__ file

__pdoc__ = {
    "_accessibility": False,
    "_async_base": False,
    "_browser": False,
    "_browser_context": False,
    "_browser_type": False,
    "_cdp_session": False,
    "_chromium_browser_context": False,
    "_connection": False,
    "_console_message": False,
    "_dialog": False,
    "_download": False,
    "_element_handle": False,
    "_event_context_manager": False,
    "_file_chooser": False,
    "_frame": False,
    "_helper": False,
    "_impl_to_api_mapping": False,
    "_input": False,
    "_js_handle": False,
    "_main": False,
    "_network": False,
    "_object_factory": False,
    "_page": False,
    "_path_utils": False,
    "_playwright": False,
    "_selectors": False,
    "_sync_base": False,
    "_transport": False,
    "_wait_helper": False,
    "_async_playwright": False,
    "_sync_playwright": False,
}

When i run from terminal playwright codegen it works


Update: The whole issue was using outdated import statement. It should be

from playwright.sync_api import sync_playwright
vitorbferna commented 3 years ago

I am having this same problem, what should I do?

Galaxyjia commented 3 years ago

cannot import name 'sync_playwright' from 'playwright.sync_api'

I can run it somedays ago,but when i update mac os to the latest.It is wrong

nileshpandey3 commented 3 years ago

I am still facing the same issue with the below import statement from playwright.sync_api import sync_playwright but when I change it to from playwright import sync_playwright i don't get the import error

josh-ashkinaze commented 2 years ago

Hi, bumping this. One issue I have is that it does not look like there is a playwright==1.8.0a1 version anymore for use with python3? I get a no version error when doing pip3 but not pip. So does this imply the package will only work in python2?

tonmoyazad commented 2 years ago
Screen Shot 2021-11-15 at 12 37 15 PM

& error I'm getting on sublime

Screen Shot 2021-11-15 at 12 38 40 PM
ShawnJorge commented 1 year ago

I'm facing same issue on Windows 10, I've tried all suggestions in this thread. Please help pLAYWRIGHT ERROR

CODE: import re

from playwright.sync_api import Playwright, expect

from playwright.sync_api import Playwright, sync_playwright, expect

from playwright import sync_playwright

from playwright.sync_api import sync_playwright

import playwright

from playwright import sync_playwright def run(playwright: sync_playwright) -> None: browser = playwright.chromium.launch(headless=False) context = browser.new_context() page = context.new_page() page.goto("https://www.edinburghlive.co.uk/newsletter-preference-centre/?utm_source=newslettersignupheader") page.get_by_role("heading", name="News", exact=True).click() page.get_by_role("button", name="Subscribe").first.click() page.get_by_role("button", name="close-icon").click() page.close()

# ---------------------
context.close()
browser.close()

with sync_playwright() as playwright: run(playwright)

ERROR: Traceback (most recent call last): File "d:\Python\PlayWright\example2.py", line 7, in from playwright.sync_api import sync_playwright File "C:\Users\User2\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright\sync_api__init.py", line 25, in import playwright.sync_api._generated File "C:\Users\User2\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright\sync_api_generated.py", line 25, in from playwright._impl._accessibility import Accessibility as AccessibilityImpl File "C:\Users\User2\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright_impl_accessibility.py", line 17, in from playwright._impl._connection import Channel File "C:\Users\User2\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright_impl_connection.py", line 23, in from greenlet import greenlet File "C:\Users\User2\AppData\Local\Programs\Python\Python311\Lib\site-packages\greenlet\init__.py", line 29, in from ._greenlet import _C_API # pylint:disable=no-name-in-module ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ImportError: DLL load failed while importing _greenlet: The specified module could not be found.

mxschmitt commented 1 year ago

@ShawnJorge try the following: https://github.com/python-greenlet/greenlet/issues/346#issuecomment-1416918053