简体   繁体   English

在 Mac 上的 Python 中使用 lxml 时收到“ImportError: cannot import name etree”

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

I'm having difficulty properly installing lxml for Python on Mac.我在 Mac 上为 Python 正确安装 lxml 时遇到了困难。 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.运行安装后,我尝试在 lxml 安装目录中运行 Test.py 以确保它正常工作。 I am immediately prompted with the error:我立即收到错误提示:

ImportError"  cannot import name etree.  

This error results from the line from lxml import etree .此错误是由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).请原谅我的无知,因为我通常不会用 Python 编程,当然也不会在 MAC 上编程(目前被迫这样做)。

For reference:以供参考:

  • Python Version: 2.7.2蟒蛇版本:2.7.2
  • Mac OS X 10.8.2苹果操作系统 X 10.8.2

Thanks in advance for all the help.在此先感谢您的所有帮助。

Sounds like you have another lxml in your path.听起来您的路径中还有另一个lxml 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'>

I had the same problem as you.我和你有同样的问题。

My problem was that I named one of my Python files lxml.py .我的问题是我将我的一个 Python 文件命名为lxml.py

You should always check if your Python file's name conflicts with a module.您应该始终检查您的 Python 文件名是否与模块冲突。

Microsoft Server Spyder IDE Python 3.7 and Python 3.9 Microsoft Server Spyder IDE Python 3.7 和 Python 3.9

Documenting this as I couldn't find the solution ANYWHERE on Google/StackOverflow etc.记录这个因为我在谷歌/StackOverflow 等上找不到任何地方的解决方案。

Regarding SharePlum, SharePoint Integration, issues with关于 SharePlum、SharePoint 集成、问题

import from lxml import etree ImportError: cannot import name etree从 lxml 导入 etree 导入错误:无法导入名称 etree

I had the same issue while trying to use SharePlum with Spyder IDE.我在尝试将 SharePlum 与 Spyder IDE 一起使用时遇到了同样的问题。

I tried installing, uninstalling, and installing versions of the below with no success pip install shareplum pip install lxml我尝试安装、卸载和安装以下版本,但均未成功 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 (eg ../Python39/Lib/Site-Packages) for packages] there seems to be a Spyder issue with Python pulling packages from other paths.我安装了 Python 3.9 和 Spyder IDE(我不知道,还安装了它自己的 Python 3.7 版本——截至 2021 年 5 月 13 日的 Spyder 版本)。[即使 Spyder IDE 设置为搜索所有 python 包清单(例如 ../Python39/Lib/Site-Packages)包] 似乎有一个 Spyder 问题,Python 从其他路径拉包。

Solution, I have uninstalled all Python and Spyder IDE installations.解决方案,我已经卸载了所有 Python 和 Spyder IDE 安装。

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.然后我只安装了 Spyder IDE(它在它的路径中嵌套了一个 python 安装。然后我进入了路径“.../Spyder/Python”,在那里你可以找到所有熟悉的 Python.exe 和其他熟悉的 Python 文件。我不得不使用此安装的 Python 来安装 Pip,然后我可以“pip install -r requirements.txt”——安装 etree lxml 和其他好东西,如 SharePlum 等。

This seemed to do the trick!这似乎可以解决问题!

I had the same problem using PyCharm IDE v2021.2 with Python 3.8我在使用 PyCharm IDE v2021.2 和 Python 3.8 时遇到了同样的问题

What worked for me was in PyCharm,对我有用的是 PyCharm,

  1. go to Preferences -> Python Interpreter,转到首选项 - > Python解释器,
  2. uninstall the lxml package,卸载 lxml 包,
  3. close & restart PyCharm,关闭并重启 PyCharm,
  4. install the lxml package安装 lxml 包

Dunno why it worked, but it did.不知道为什么它起作用了,但它确实起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM