模块导入错误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 ...
答
TextMate很可能使用与您的终端不同的PYTHONPATH。我不是TextMate用户,所以我无法帮助您,但它应该指向正确的方向。
答
您可能正在从TextMate运行不同版本的Python。我有一个类似的问题与RedHat有2个版本的Python。我已经将模块安装到一个模块,但试图与另一个模块执行。
这似乎是一个完美的解决方案 – thchand