在lxml.etree中无法解析的导入字符串



我在mac上安装了lxml,并试图在我的代码中使用它,我在导入tostring和tounicode时遇到了错误。蟒蛇根本看不到它。你知道我做错了什么吗?

下面是导致问题的代码-

from lxml.etree import tostring
from lxml.etree import tounicode

我得到一个无法解决的导入错误

我的IDE (eclipse)也能够看到lxml的init.py文件。etree模块。下面是它看到的——

# this is a package
def get_include():
    """
    Returns a list of header include paths (for lxml itself, libxml2
    and libxslt) needed to compile C code against lxml if it was built
    with statically linked libraries.
    """
    import os
    lxml_path = __path__[0]
    include_path = os.path.join(lxml_path, 'includes')
    includes = [include_path, lxml_path]
    for name in os.listdir(include_path):
        path = os.path.join(include_path, name)
        if os.path.isdir(path):
            includes.append(path)
    return includes

谢谢你的帮助。

编辑:
我看到的唯一原木是未解析的import: tostring未解析的import: tounicode

当我在导入tostring之前添加以下行时,它没有错误——从lxml中导入树

也给你更多的背景,我正在努力做什么。我得到了可读性代码从这里(https://github.com/buriy/python-readability),并试图在我的项目中使用它。

编辑2:我解决了问题,但仍然不明白为什么。我想直接使用可读性项目中的代码,而不用使用easy_install安装包。这个想法是进入代码,这样我就能理解它在做什么。但是当我将代码复制到我的项目中时,我在可读性代码中得到了上述错误。如果我使用easy_install安装包,那么我可以简单地导入由可读性导出的类并使用它。

那么谁能告诉我直接使用代码和安装包的区别是什么?什么是.egg文件?如何创建一个?

在lxml的代码中,它动态加载模块。这使得IDE无法分析引用,因为IDE只分析原始代码。

当我试图在Intellij IDEA Python项目中解决资源和导入etree时,我发现这个解决方案非常有用:

查看lxml教程,该教程给出了解决方案:

try:
    from lxml import etree
    print("running with lxml.etree")
except ImportError:
    try:
        # Python 2.5
        import xml.etree.cElementTree as etree
        print("running with cElementTree on Python 2.5+")
    except ImportError:
        try:
            # Python 2.5
            import xml.etree.ElementTree as etree
            print("running with ElementTree on Python 2.5+")
        except ImportError:
            try:
                # normal cElementTree install
                import cElementTree as etree
                print("running with cElementTree")
            except ImportError:
                try:
                    # normal ElementTree install
                    import elementtree.ElementTree as etree
                    print("running with ElementTree")
                except ImportError:
                    print("Failed to import ElementTree from any known place")

最新更新