【问题标题】:Cannot import name 'etree' in Python 3.7, how can I get this working?无法在 Python 3.7 中导入名称“etree”,我怎样才能让它工作?
【发布时间】:2020-03-12 12:31:10
【问题描述】:

我正在处理Automate the Boring Stuff with Python, Chapter 13,但不知道如何让 python-docx 模块正常工作。当我尝试导入它ImportError: cannot import name 'etree' from 'lxml' (<path>,__init__.py)。尝试from lxml import etree 也不起作用。我已经在这几天了,刚刚安装了 Anaconda,其中包括 lxml 所需的 libxml2 和 libxslt,但仍然没有。有任何想法吗?我非常迷茫,整个过程涉及大约 4 或 5 个不同的文档,我无法理解其中的任何一个。

这是我尝试过的几个StackOverflow问题:

我还找到了一个.exe,但它适用于 Python 2.7。

我在 Windows 10 上使用 Python 3.7 64 位。

除了上述解决方案之外,我已将这些软件包卸载并重新安装在两个不同的目录中,但无济于事。

好的 furas,谢谢你的提示,这里是 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.

【问题讨论】:

  • 你是如何安装 lxml 的? pip install lxml?还有什么办法? conda install?
  • 可以自己导入lxml吗?您是否按照 Anaconda/Conda 文档中的任何示例来管理软件包?在 Conda 提示符(?)中看起来像 pip install lxmlpip install --upgrade lxml。就在几天前,我在 Powershell 提示符下使用python -m pip install --upgrade lxml 进行了升级,from lxml import etree 工作正常(尽管我没有在这台计算机上安装 Anaconda)。
  • @zvone pip install lxml,我也使用了-t &lt;directory&gt;,因为我有一些不同的模块没有安装在正确的site packages目录中的问题,然后当我尝试Anaconda时我使用了conda install @wwii 我刚刚尝试了这两个, lxml 是最新的。我可以导入lxml,但不能导入etree
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
  • 您可以在lfd.uci.edu/~gohlke/pythonlibs/#lxml获得预编译轮子

标签: python lxml docx


【解决方案1】:

也许你已经安装了 Anaconda 和 Python。 这种情况会导致 Python 脚本之间发生冲突。 解决方案是从 Python 安装位置删除 Python 脚本和包。 它可能位于 C:\programfiles\Python(如果全局安装)或 %USER\AppData\Roaming\Python。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-25
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 2018-03-21
    • 2022-01-23
    相关资源
    最近更新 更多