gawel / pyquery

A jquery-like library for python
http://pyquery.rtfd.org/
Other
2.3k stars 182 forks source link

Python 3.12 fails to import pyquery with "ImportError: cannot import name 'etree' from 'lxml' " #255

Open edcarstens opened 7 months ago

edcarstens commented 7 months ago

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 from requests_html import HTMLSession File "C:\users\edcar\AppData\Roaming\Python\Python312\site-packages\requests_html.py", line 11, in from pyquery import PyQuery File "C:\python312\lib\site-packages\pyquery__init.py", line 5, in from .pyquery import PyQuery # NOQA ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python312\lib\site-packages\pyquery\pyquery.py", line 12, in from lxml import etree ImportError: cannot import name 'etree' from 'lxml' (C:\python312\lib\site-packages\lxml\init__.py)

gawel commented 7 months ago

tests are green https://github.com/gawel/pyquery/actions/runs/8529082816/job/23364099065

edcarstens commented 7 months ago

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 from requests_html import HTMLSession File "C:\users\edcar\AppData\Roaming\Python\Python312\site-packages\requests_html.py", line 11, in from pyquery import PyQuery File "C:\python312\lib\site-packages\pyquery__init.py", line 5, in from .pyquery import PyQuery # NOQA ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python312\lib\site-packages\pyquery\pyquery.py", line 12, in from lxml import etree ImportError: cannot import name 'etree' from 'lxml' (C:\python312\lib\site-packages\lxml\init__.py)

gawel commented 7 months ago

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