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.
n_b
  • 173
  • 2
  • 4
  • 12
  • How did you install lxml? `pip install lxml`? Some other way? `conda install`? – zvone Nov 16 '19 at 16:06
  • 1
    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). – wwii Nov 16 '19 at 16:18
  • @zvone ```pip install lxml```, and I also used ```-t ``` 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``` @wwii I just tried both of those, lxml is up to date. I can import ```lxml``` but not ```etree```. – n_b Nov 16 '19 at 16:49
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Nov 16 '19 at 16:56
  • 1
    You can get pre-compiled wheels at https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml – Joe Nov 16 '19 at 18:06
  • I installed wheels, that didn't help. – n_b Nov 17 '19 at 02:29

1 Answers1

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.