模块导入错误Python

模块导入错误Python

问题描述:

我刚刚安装了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有什么关系?

建议请!

这很可能意味着你有一个以上的python安装在系统上,并且TextMate的和终端使用不同的人通过默认。

一个解决方法:在你的Python文件,你可以指定一个解释指令指向您选择的Python安装(和执行):

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

这似乎是一个完美的解决方案 – thchand

TextMate很可能使用与您的终端不同的PYTHONPATH。我不是TextMate用户,所以我无法帮助您,但它应该指向正确的方向。

您可能正在从TextMate运行不同版本的Python。我有一个类似的问题与RedHat有2个版本的Python。我已经将模块安装到一个模块,但试图与另一个模块执行。

您需要在TextMate的设置中定义shell变量,特别是'TM_PYTHON'需要指向您的Python二进制文件。

要查找的Python的使用,在终端,您可以键入“这蟒蛇”

+0

TM_PYTHON变量值将是:/库/框架/ Python的.framework/Versions/2.7/bin/python? – thchand

+0

Mine位于'/ usr/bin/python',但它最终取决于您的特定系统。 – joet3ch