1

1.I am using Python 2.6.6
2.Downloaded BeautifulSoaup and lxml external parser , unpackaged it and extract bs4 and lxml folder.
3.write the link.py as below

#! /usr/bin/python

from bs4 import BeautifulSoup
from urllib import urlopen
from lxml import etree

webpage = urlopen('http://www.google.com')
soup=BeautifulSoup(webpage, "lxml")
for link in soup.find_all('a'):
print(link.get('href'))

While running the ./link.py giving the following error :

Traceback (most recent call last):
  File "./link.py", line 5, in <module>
    from lxml import etree
ImportError: cannot import name etree
user3256223
  • 11
  • 1
  • 2
  • `pip install lxml==3.1.2` – Gaurav Jain Sep 23 '14 at 10:27
  • Is lxml 3.4 broken ? I have the same problem ! lxml is installed both for python 2 & 3, but I can't do `from lxml import etree` – Gilles Quénot Dec 26 '14 at 18:13
  • The possible reason for this: file you running named "lxml.py", so etree cannot be imported from the same place where it's importing from. P.S. It's not your case, but may be the cause for someone, who googled it here: Change your script name from lxml.py to something else! – ScudS3 Feb 07 '17 at 13:07
  • https://stackoverflow.com/questions/13355984/get-errors-when-import-lxml-etree-to-python – krenerd May 13 '21 at 00:25

2 Answers2

1

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 requirements.txt" - which installs etree lxml and other goodies such as SharePlum etc.

This seemed to do the trick!

Good luck

  • https://stackoverflow.com/questions/13355984/get-errors-when-import-lxml-etree-to-python Maybe this would work? – krenerd May 13 '21 at 00:26
-3

Why are you using lxml if you have beautifulsoup? (And visa versa) The lxml library import depends on the python version. Take a look at this website: http://lxml.de/tutorial.html

Further more a couple of hints which make programming a lot easier:

  1. Try PIP. It makes installing packages a lot easier. Just by typing "pip install SomePackage" it downloads and installs the package.

  2. Try using requests it's more readible than urllib.

  3. Trying to get http://www.google.com will give some errors, because google doesn't want you to do that. Try using http://www.example.com instead.

Vincent Beltman
  • 2,064
  • 13
  • 27