Python 3.12 fails to import pyquery with "ImportError: cannot import name 'etree' from 'lxml' "
Python 3.12 fails to import pyquery. The specific problem reported is:
ImportError: cannot import name 'etree' from 'lxml'
Here is the full error message I get when trying to run a simple test script that imports from requests_html:
$ python test.py
Traceback (most recent call last):
File "C:\Users\edcar\OneDrive\Documents\voterRolls\test.py", line 7, in
tests are green https://github.com/gawel/pyquery/actions/runs/8529082816/job/23364099065
My guess is it is a backwards compatibility issue with lxml. If so, you simply have to change the lxml version to whatever version works correctly. The python installer I use is pip. To reproduce it run 'pip install pyquery' and then try importing it in a test script.
$ pip install pyquery Collecting pyquery Obtaining dependency information for pyquery from https://files.pythonhosted.org/packages/36/b7/f7ccf9e52e2817e1265d3719c600fa4ef33c0 7de4d5ef0ced3f43ab1cef2/pyquery-2.0.0-py3-none-any.whl.metadata Using cached pyquery-2.0.0-py3-none-any.whl.metadata (9.0 kB) Requirement already satisfied: lxml>=2.1 in c:\python312\lib\site-packages (from pyquery) (5.2.1) Requirement already satisfied: cssselect>=1.2.0 in c:\python312\lib\site-packages (from pyquery) (1.2.0) Using cached pyquery-2.0.0-py3-none-any.whl (22 kB) Installing collected packages: pyquery Successfully installed pyquery-2.0.0
[notice] A new release of pip is available: 23.2.1 -> 24.0 [notice] To update, run: C:\Python312\python.exe -m pip install --upgrade pip
DESKTOP-T28NKOE ~/OneDrive/Documents/voterRolls
$ python test.py
Traceback (most recent call last):
File "C:\Users\edcar\OneDrive\Documents\voterRolls\test.py", line 7, in
The test install lxml==5.2.1 which il the latest. So I don't really get your point
Try a script with "from lxml import etree" and I guess you'll have the same problem. Seems like this has nothing to do with pyquery