python - 模块导入错误 Python

标签 python module lxml importerror

我刚刚安装了 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/

相关文章:

python - 如何在Python中正确分离数据采集、处理和可视化?

python - R、statmodels、sklearn 与逻辑回归分类任务的比较

javascript - Typescript 2 — 使用 ES6 导入和要求?

python - 在解析 XML 文件时,有没有办法使用 lxml.etree 跳过第一个条目或在特定子项处开始迭代?

python - 使用 lxml 时 https 有什么问题?

python - 使用 pandas 从 CSV 加载随机样本

python - Google Sheets API 附加额外的撇号

python - 为什么 PyCharm 无法找到正确版本的 pip 来安装 Python 模块?

python - 如何从当前文件夹旁边的文件夹导入模块?

python-2.7 - src/lxml/etree_defs.h :9:31: fatal error: libxml/xmlversion. h:没有这样的文件或目录