thchand
thchand

Reputation: 368

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

Upvotes: 1

Views: 2011

Answers (4)

miku
miku

Reputation: 188194

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

Upvotes: 3

joet3ch
joet3ch

Reputation: 2512

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'

Upvotes: 2

John Giotta
John Giotta

Reputation: 16984

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.

Upvotes: 0

mluebke
mluebke

Reputation: 8819

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.

Upvotes: 1

Related Questions