2013-03-29 37 views
0

我在mac上安装了lxml并试图在我的代码中使用它,并且导入了tostring和tounicode错误。 Python根本看不到它。任何想法我做错了什么?lxml.etree中未解决的导入tostring

这是导致问题的代码 - 我得到一个未解决的导入错误

而且我的IDE(日蚀)是能够看到LXML的初始化 .py文件

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

。 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 

感谢您的任何帮助。

编辑:
中仅有的日志我看到的是 未解决进口:的toString 未解决进口:tounicode

当我的ToString它的导入前添加以下行工作没有错误 - 从 进口etree lxml

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

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

因此,有人可以告诉我直接使用代码和安装软件包有何区别吗?什么是.egg文件?如何创建一个?

+0

你能够导入_just_'lxml'吗?你能发布你看到的完整例外吗? –

+0

@ dan.lococq - 我编辑了我的问题并添加了更多信息。是的,我能够导入lxml – R11

回答

0

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

0

我发现这个解决方案试图解决资源,在我的IntelliJ IDEA的Python项目导入etree时非常有用:在lxml tutorial

看一看这表明此解决方案:

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