简体   繁体   中英

unresolved import tostring in lxml.etree

I installed lxml on a mac and trying to use it in my code and I get errors importing tostring and tounicode. Python cannot see it at all. Any ideas what I am doing wrong?

Here is the code that is causing problems -

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

I get an unresolved import error

Also my IDE (eclipse) is able to see the init .py file for lxml.etree module. Here is what it sees ---

# 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

Thanks for any help.

EDIT:
The only log I see is Unresolved import: tostring Unresolved import: tounicode

And when I add the following line before the import of tostring it works no errors -- import etree from lxml

Also to give you some more background on what I am trying to do. I got the readability code from here ( https://github.com/buriy/python-readability ) and trying to use it in my project.

EDIT2: I fixed the problem but still do not understand why. I wanted to use the code from the readability project directly without installing the package using easy_install. The idea was step into the code so that I understand what its doing. But when I copy the code into my project I get the above mentioned error in the readability code. If I install the package using easy_install then all I can simply import the class exported by readability and use it.

So can some one tell me what the difference is between using the code directly and the package installed? What is a .egg file? How to create one?

In lxml's code, it dynamically load modules. That makes IDE fails to analyze reference as IDE just analyzes raw code.

I've found this solution extremely useful when trying to resolve the resource and import etree in my Intellij IDEA Python project:

Have a look at lxml tutorial which suggests this solution:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM