[英]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
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.