Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
edcarstens opened this issue Apr 15, 2024 · 3 comments

Comments

@edcarstens
Copy link

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
Copy link
Owner

gawel commented Apr 15, 2024

@edcarstens
Copy link
Author

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
Copy link
Owner

gawel commented Apr 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants