2011-09-26 220 views
1

我剛剛安裝了lxml用於解析python中的xml文件。我使用TextMate作爲IDE。問題是,當我嘗試導入lxml,(from lxml import entree)然後我得到模塊導入錯誤Python

導入錯誤:「無模塊名爲LXML」

但是當我使用終端然後一切都很好

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from lxml import etree 
>>> root=etree.element("root") 
>>> root=etree.Element("root") 
>>> print (root.tag) 
root 
>>> root.append(etree.Element("child1")) 
>>> child2 = etree.SubElement(root, "child2") 
>>> child3 = etree.SubElement(root, "child3") 
>>> print (etree.tostring(root,pretty_print=True)) 
<root> 
    <child1/> 
    <child2/> 
    <child3/> 
</root> 

這是很奇怪。它與TextMate有什麼關係?

建議請!

回答

3

這很可能意味着你有一個以上的python安裝在系統上,並且TextMate的和終端使用不同的人通過默認。

一個解決方法:在你的Python文件,你可以指定一個解釋指令指向您選擇的Python安裝(和執行):

#!/usr/local/bin/python 
# Even thought standard python is in /usr/bin/python, here we want another ... 
+0

這似乎是一個完美的解決方案 – thchand

1

TextMate很可能使用與您的終端不同的PYTHONPATH。我不是TextMate用戶,所以我無法幫助您,但它應該指向正確的方向。

0

您可能正在從TextMate運行不同版本的Python。我有一個類似的問題與RedHat有2個版本的Python。我已經將模塊安裝到一個模塊,但試圖與另一個模塊執行。

2

您需要在TextMate的設置中定義shell變量,特別是'TM_PYTHON'需要指向您的Python二進制文件。

要查找的Python的使用,在終端,您可以鍵入「這蟒蛇」

+0

TM_PYTHON變量值將是:/庫/框架/ Python的.framework/Versions/2.7/bin/python? – thchand

+0

Mine位於'/ usr/bin/python',但它最終取決於您的特定系統。 – joet3ch