2013-03-29 38 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")