Cannot import name 'etree' in Python 3.7, how can I get this working?
Asked Answered
S

1

6

I'm working through Automate the Boring Stuff with Python, Chapter 13, and can't figure out how to get the python-docx module working. When I try to import it ImportError: cannot import name 'etree' from 'lxml' (<path>,__init__.py). Trying from lxml import etree doesn't work either. I've been at this for days, just installed Anaconda which includes libxml2 and libxslt which are required for lxml and still nothing. Any ideas? I'm extremely lost, there's about 4 or 5 different docs involved with this entire process and I can't make sense of any of it.

Here's a couple of StackOverflow questions I've tried bits and pieces from:

I also found a .exe but it was for Python 2.7.

I'm using Python 3.7 64 bit on Windows 10.

Besides the solutions above I have uninstalled and reinstalled these packages in two different directories to no avail.

Okay furas, thank you for the tip, here is the Traceback.

C:\Users\...>py
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'etree' from 'lxml' (C:\Users\...\AppData\Roaming\Python\Python37\site-packages\lxml\__init__.py)


**MY PERSONAL SOLUTION:**
After messing with this for three days I decided to uninstall every Python release I had on my PC and do a clean install. That was the only way for me to get it working, but it worked! No surprises there I think. I'm assuming there was a problem with how I had Python installed because I had two separate directories with different releases and pip was installing packages in a weird way. It doesn't make sense to me but the problem is solved on my end. I don't know if this will help anyone else.
Sinhalese answered 16/11, 2019 at 15:56 Comment(6)
How did you install lxml? pip install lxml? Some other way? conda install?Stramonium
Can you import lxml itself? Have you followed any of the examples in the Anaconda/Conda docs for managing packages? Looks like pip install lxml or pip install --upgrade lxml in a Conda prompt(?). Just a couple days ago I upgraded with python -m pip install --upgrade lxml from a Powershell prompt and from lxml import etree works fine (albeit I don't have Anaconda installed on this computer).Consequently
@Stramonium pip install lxml, and I also used -t <directory> because I had some issues with a different module not installing in the right site packages directory, then when I tried Anaconda I used conda install @Consequently I just tried both of those, lxml is up to date. I can import lxml but not etree.Sinhalese
always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information.Peag
You can get pre-compiled wheels at lfd.uci.edu/~gohlke/pythonlibs/#lxmlRonald
I installed wheels, that didn't help.Sinhalese
R
0

maybe you have installed both Anaconda and Python. this case causes confliction between Python scripts. the solution is deleting Python scripts and packages from Python installed location. it may be on C:\programfiles\Python (if installed globally) or %USER\AppData\Roaming\Python.

Ruel answered 11/10, 2020 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.