Python ImportError when executing 'import.py', but not when executing 'python import.py'

Posted by Martin Del Vecchio on Stack Overflow See other posts from Stack Overflow or by Martin Del Vecchio
Published on 2010-03-12T15:23:01Z Indexed on 2010/03/12 15:37 UTC
Read the original article Hit count: 829

Filed under:
|

I am running Cygwin Python version 2.5.2.

I have a three-line source file, called import.py:

#!/usr/bin/python
import xml.etree.ElementTree as ET
print "Success!"

When I execute "python import.py", it works:

C:\Temp>python import.py
Success!

When I run the python interpreter and type the commands, it works:

C:\Temp>python
Python 2.5.2 (r252:60911, Dec  2 2008, 09:26:14)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> #!/usr/bin/python
... import xml.etree.ElementTree as ET
>>> print "Success!"
Success!
>>>

But when I execute "import.py', it does not work:

C:\Temp>which python
/usr/bin/python

C:\Temp>import.py
Traceback (most recent call last):
  File "C:\Temp\import.py", line 2, in ?
    import xml.etree.ElementTree as ET
ImportError: No module named etree.ElementTree

When I remove the first line (#!/usr/bin/python), I get the same error. I need that line in there, though, for when this script runs on Linux. And it works fine on Linux.

Any ideas?

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about import