开发者

Module import Error Python

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.app开发者_JAVA百科end(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!


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 ...


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'


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜