Craig Alleva
Craig Alleva

Reputation: 131

Receiving 'ImportError: cannot import name etree' when using lxml in Python on Mac

I'm having difficulty properly installing lxml for Python on Mac. I have followed the instructions here, which after installation indicates that the installation is successful (however, there are some warnings. The full log of the install and warnings can be found here)

After running the install, I am trying to run Test.py in the lxml install directory to ensure that it's working correctly. I am immediately prompted with the error:

ImportError"  cannot import name etree.  

This error results from the line from lxml import etree.

I can't seem to figure out why it's failing here after a seemingly successful install. Please forgive my ignorance, as I don't typically program in Python and certainly never on a MAC (forced to do so at the moment).

For reference:

Thanks in advance for all the help.

Upvotes: 13

Views: 20628

Answers (4)

Winston Lee
Winston Lee

Reputation: 107

I had the same problem using PyCharm IDE v2021.2 with Python 3.8

What worked for me was in PyCharm,

  1. go to Preferences -> Python Interpreter,
  2. uninstall the lxml package,
  3. close & restart PyCharm,
  4. install the lxml package

Dunno why it worked, but it did.

Upvotes: 0

Microsoft Server Spyder IDE Python 3.7 and Python 3.9

Documenting this as I couldn't find the solution ANYWHERE on Google/StackOverflow etc.

Regarding SharePlum, SharePoint Integration, issues with

import from lxml import etree ImportError: cannot import name etree

I had the same issue while trying to use SharePlum with Spyder IDE.

I tried installing, uninstalling, and installing versions of the below with no success pip install shareplum pip install lxml

I had Python 3.9 installed and Spyder IDE (which I didn't know, also installs it's own version of Python 3.7 - as at 13 May 2021 Spyder version) .. [even though the Spyder IDE is set up to search all python package inventories (e.g. ../Python39/Lib/Site-Packages) for packages] there seems to be a Spyder issue with Python pulling packages from other paths.

Solution, I have uninstalled all Python and Spyder IDE installations.

I then ONLY installed Spyder IDE (which nests a python install within it's PATH. I then went into the PATH ".../Spyder/Python" where you can find all the familiar Python.exe and other familiar Python files. I had to use this install of Python to install Pip, and then I could "pip install -r requirements.txt" - which installs etree lxml and other goodies such as SharePlum etc.

This seemed to do the trick!

Upvotes: 0

Shuai Li
Shuai Li

Reputation: 2766

I had the same problem as you.

My problem was that I named one of my Python files lxml.py.

You should always check if your Python file's name conflicts with a module.

Upvotes: 4

Yuval Adam
Yuval Adam

Reputation: 165242

Sounds like you have another lxml in your path. Make sure you are referencing the right one, it should look something like this:

>>> import lxml
>>> lxml
<module 'lxml' from '/path/to/lib/python2.7/site-packages/lxml/__init__.pyc'>

Upvotes: 17

Related Questions