模块导入错误Python
我刚刚安装了 lxml 用于在 python 中解析 xml 文件。我使用 TextMate 作为 IDE。问题是,当我尝试导入 lxml (from lxml import entree)
时,我得到
ImportError:'No module named 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有关系吗?
请提出建议!
I just installed lxml for parsing xml file in python. I am using TextMate as an IDE. Problem is that when I try to import lxml (from lxml import entree)
then I get
ImportError:'No module named lxml'
But when I use Terminal then everything is fine
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>
It's pretty weird. Does it have something to do with TextMate?
Suggestion Please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很可能意味着您的系统上安装了多个 Python,并且 TextMate 和终端默认使用不同的 Python。
一种解决方法:在 python 文件中,您可以指定一个解释器指令来指向您选择的 python 安装(和可执行文件):
This most probably means that you have more than one python installation on your system and that TextMate and the Terminal using different ones by default.
One workaround: In your python file, you can specify an interpreter directive to point to the python installation (and executable) of your choice:
您需要在 TextMate 的设置中定义 shell 变量,特别是“TM_PYTHON”需要指向您的 Python 二进制文件。
要查找您使用的 Python,您可以在终端中输入“which python”
You need to define the shell variables in TextMate's settings, specifically 'TM_PYTHON' needs to point to your Python binary.
To find which Python your using, in a terminal you could type 'which python'
TextMate 可能使用与您的终端不同的 PYTHONPATH。我不是 TextMate 用户,所以我无法帮助您,但它应该为您指明正确的方向。
It's likely that TextMate is using a different PYTHONPATH than your terminal. I'm not a TextMate user so I can't help you there, but it should point you in the right direction.
您可能正在运行与 TextMate 不同版本的 Python。我在使用具有 2 个版本的 Python 的 RedHat 时也遇到了类似的问题。我已将该模块安装到一个模块上,但正在尝试使用另一个模块来执行。
You might be be running a different version of Python from TextMate. I had a similar issue with RedHat having 2 versions of Python. I had installed the module to one, but was trying to execute with another.