无法在 Python 3.7 中导入名称'etree',如何使其工作?



我正在学习《用Python自动化无聊的东西》,第13章,不知道如何让Python docx模块工作。当我尝试导入它ImportError: cannot import name 'etree' from 'lxml' (<path>,__init__.py)时。尝试from lxml import etree也不起作用。我已经做了好几天了,刚刚安装了Anaconda,它包括libxml2和libxslt,这是lxml所必需的,但仍然没有。有什么想法吗?我非常失落,整个过程涉及大约4到5个不同的文档,我无法理解其中的任何一个

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

  • 在Windows 10上安装lxml、libxml2、libxslt for Python 3.5-这是我决定安装Anaconda的地方,感谢furas的回答
  • LXML:无法导入etree-从这里我尝试安装不同的轮子。我已经从pip安装了lxml,所以这并不重要,但我学到了一些关于车轮的知识,所以这很好

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

我在Windows10上使用的是Python 3.7 64位。

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

好的,弗拉斯,谢谢你的提示,这是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...AppDataRoamingPythonPython37site-packageslxml__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.

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

最新更新