我刚刚安装了 lxml 用于在 python 中解析 xml 文件。我正在使用 TextMate 作为 IDE。问题是,当我尝试导入 lxml (from lxml import entree)
时,我得到了
ImportError:'没有名为 lxml 的模块'
但是当我使用 Terminal 时一切都很好
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 ...
关于python - 模块导入错误 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7557950/