ImportError: cannot import name 'sync_playwright' from 'playwright'
Fixes the Playwright import failure where the package imports but has no sync_playwright inside. Use when `from playwright.sync_api import sync_playwright` raises ImportError naming sync_playwright. Not for `No module named 'playwright'` or the browser Executable doesn't exist error.
Nine times out of ten you have a file named playwright.py in your working directory shadowing the real package. Rename it (and clear __pycache__) and the import works. If there is no such file, the install is broken: python -m pip install --force-reinstall playwright restores the missing names.
The error
ImportError: cannot import name 'sync_playwright' from 'playwright'Fix it
- Check what got imported as playwright
python -c "import playwright; print(playwright.__file__)"Expected: a site-packages path ending in playwright/__init__.py. A path in your project ending in playwright.py is the shadowing file.
- Rename the shadowing file
Rename your playwright.py to something like playwright_demo.py, delete the __pycache__ folder next to it, then:
python -c "from playwright.sync_api import sync_playwright; print('ok')"Expected: prints ok. The script directory beats site-packages in import order, so your file always won.
- If nothing shadows it, reinstall
python -m pip install --force-reinstall playwrightExpected: the import from step 2 now works. A partial install can leave a playwright directory without the sync_api subpackage.
When this applies
from playwright.sync_api import sync_playwright(or async_api) raises ImportError naming the function- your script sits next to a file called
playwright.py - the import works everywhere except your project folder
When it does NOT apply
ModuleNotFoundError: No module named 'playwright'means nothing installedExecutable doesn't existhappens later, at browser launchcannot import name 'stealth_sync' from 'playwright_stealth'is the separate stealth package
Compatibility
playwright 1.x Python, all versions. The shadowing trap is pure import semantics and version-independent.
Variant phrasings
ImportError: cannot import name 'async_playwright' from 'playwright'
The async twin. Same cause, same fix.
ImportError: cannot import name 'sync_playwright' from 'playwright' (unknown location)
The (unknown location) suffix means Python found a namespace package with no code, typical of a broken or namespace-shadowed install. Reinstall per step 3.
Why it happens
Python imports from the script's directory before site-packages. A file named playwright.py next to your script becomes the playwright module, and it naturally contains no sync_playwright. The less common variant is a genuinely broken install where the sync_api subpackage is missing from site-packages.
Edge cases
- Check for
playwright/directories too, not justplaywright.py. A stray folder shadows the same way. - In notebooks, the kernel's working directory matters, not the notebook's folder.
os.getcwd()tells you where to look. - If you installed with
pip install playwrightwhile a conda env was active but run the script with system Python, that is the two-interpreter problem, not shadowing.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.