模块导入错误Python

发布于 2024-12-06 13:58:03 字数 975 浏览 0 评论 0原文

我刚刚安装了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-12-13 13:58:03

这很可能意味着您的系统上安装了多个 Python,并且 TextMate 和终端默认使用不同的 Python。

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

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

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:

#!/usr/local/bin/python
# Even thought standard python is in /usr/bin/python, here we want another ...
腹黑女流氓 2024-12-13 13:58:03

您需要在 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'

少年亿悲伤 2024-12-13 13:58:03

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.

贵在坚持 2024-12-13 13:58:03

您可能正在运行与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文