ImportError: cannot import name 'PyQuery' from 'pyquery'
Fixes the pyquery shadowing failure where your own pyquery.py hides the library. Use when `from pyquery import PyQuery` raises ImportError naming PyQuery even though pyquery is installed. Not for missing pyquery.
A file named pyquery.py in your working directory is shadowing the installed package. Rename it (and delete __pycache__), and the import works. The pyquery maintainer flags this exact trap in the issue thread.
The error
ImportError: cannot import name 'PyQuery' from 'pyquery'Fix it
- Confirm the shadowing
python -c "import pyquery; print(pyquery.__file__)"Expected: a project path ending in pyquery.py means your file is the problem. site-packages means the install is genuinely broken.
- Rename your file
Rename pyquery.py to something like query_demo.py, clear __pycache__, then:
python -c "from pyquery import PyQuery; print('ok')"Expected: prints ok.
- If nothing shadows it, reinstall
python -m pip install --force-reinstall pyqueryExpected: the import works. A damaged install can leave a pyquery directory without the class.
When this applies
from pyquery import PyQueryraises ImportError namingPyQuery- pyquery is installed but your script sits next to
pyquery.py
When it does NOT apply
No module named 'pyquery'is the missing installcannot import name 'etree' from 'lxml'is the old-lxml-on-3.12 problem
Compatibility
All pyquery versions. Pure import-semantics trap.
Why it happens
The script's directory beats site-packages in import order, so pyquery.py next to your script becomes the pyquery module. Your file defines no PyQuery class, so the from-import fails. Same family as the selenium.py / playwright.py / newspaper.py traps.
Edge cases
- The cached
__pycache__/pyquery.*.pyckeeps shadowing after a rename. Delete the cache dir.
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.