简体   繁体   English

Windows上的lxml错误 - AttributeError:模块'lxml'没有属性'etree'

[英]lxml error on Windows - AttributeError: module 'lxml' has no attribute 'etree'

I am using Anaconda v4.2 with Python 3.5 on Windows 32 bit, and wanting to use lxml etree. 我在Windows 32位上使用Anaconda v4.2和Python 3.5,并且想要使用lxml etree。 My Anaconda distribution includes lxml 3.6.4, but the only lxml function that my IDE (PyCharm, although I'm getting the same error when running the code with Jupyter Notebook) can see is get_include(). 我的Anaconda发行版包括lxml 3.6.4,但是我的IDE(PyCharm,虽然我在使用Jupyter Notebook运行代码时遇到同样的错误)的唯一lxml函数可以看到是get_include()。 The following code: 以下代码:

import lxml
full_xml_tree = lxml.etree.parse('myfile.xml')

just gives me the error: 只是给我错误:

AttributeError: module 'lxml' has no attribute 'etree'

I also tried installing the VisualC++ compiler for Windows, but that hasn't made any difference. 我也尝试过为Windows安装VisualC ++编译器,但这没有任何区别。 I tried reinstalling lxml using conda on the command line, again no change to my error. 我尝试在命令行上使用conda重新安装lxml,再次没有更改我的错误。 What am I missing? 我错过了什么? It seems like the lxml.get_include() function isn't finding any of the files to include, and I don't really understand how the etree.cp35-win32.pyd file (which I assume contains the compiled etree code??) should be being associated with the lxml package. 好像lxml.get_include()函数没有找到要包含的任何文件,我真的不明白etree.cp35-win32.pyd文件(我假设它包含编译的etree代码?)应该与lxml包相关联。 Any help much appreciated! 任何帮助非常感谢!

Cathy 凯茜

This is a bit of a quirk in how the etree (ElementTree) subpackage is imported. 这是如何导入etree (ElementTree)子包的一个怪癖。

You have to import the subpackage explicitly for it to be available: 您必须明确导入子包才能使其可用:

import lxml.etree
full_xml_tree = lxml.etree.parse('myfile.xml')

The recommended way to achieve what you're trying to do would be to import the ElementTree module: 实现您尝试执行的操作的推荐方法是导入ElementTree模块:

import xml.etree.ElementTree as ET
tree = ET.parse('myfile.xml')

See: https://docs.python.org/3.6/library/xml.etree.elementtree.html 请参阅: https//docs.python.org/3.6/library/xml.etree.elementtree.html

Why does this happen? 为什么会这样?

Imagine a package with a directory structure like this: 想象一个包含这样的目录结构的包:

test_pkg/__init__.py
test_pkg/shown_module.py
test_pkg/hidden_module.py

and where the __init__.py contains the following: 以及__init__.py包含以下内容:

from . import shown_module

using this package you use shown_module directly: 使用这个包你直接使用shown_module

>>> import test_pkg
>>> test_pkg.shown_module
<module 'test_pkg.shown_module' from '.../test_pkg/shown_module.py'>

But hidden_module can't be used directly: 但是hidden_module不能直接使用:

>>> test_pkg.hidden_module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'test_pkg' has no attribute 'hidden_module'

But it can be used if imported: 但是如果导入它可以使用它:

>>> import test_pkg.hidden_module
>>> test_pkg.hidden_module
<module 'test_pkg.hidden_module' from '.../test_pkg/hidden_module.py'>

However, I do not know why ElementTree is "hidden". 但是,我不知道为什么ElementTree是“隐藏的”。

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

相关问题 AttributeError:“ lxml.etree.QName”对象没有属性“ resolve” - AttributeError: 'lxml.etree.QName' object has no attribute 'resolve' lxml: cssselect(): AttributeError: &#39;lxml.etree._Element&#39; 对象没有属性 &#39;cssselect&#39; - lxml: cssselect(): AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect' ImportError:没有名为lxml.etree的模块(Windows) - ImportError: No module named lxml.etree ( windows ) 在 lxml 中使用 etree 时出错 - Error using etree in lxml 'lxml.etree._ElementTree' object 没有属性'插入' - 'lxml.etree._ElementTree' object has no attribute 'insert' &#39;lxml.etree._ElementTree&#39;对象没有属性&#39;cssselect&#39; - 'lxml.etree._ElementTree' object has no attribute 'cssselect' Pylint错误消息:“E1101:模块'lxml.etree'没有'strip_tags'成员'” - Pylint Error Message: “E1101: Module 'lxml.etree' has no 'strip_tags' member'” python lxml 3.3.5 - 加载代码时出错 - “ValueError:lxml.etree._Element的大小错误,请尝试重新编译” - python lxml 3.3.5 - error on loading code - “ValueError: lxml.etree._Element has the wrong size, try recompiling” lxml.etree.XMLSyntaxError:没有属性声明 - lxml.etree.XMLSyntaxError: No declaration for attribute 导入错误:没有名为 lxml.etree 的模块 - ImportError: No module named lxml.etree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM