Martin Del Vecchio
Martin Del Vecchio

Reputation: 3818

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

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.

Upvotes: 2

Views: 1623

Answers (4)

Turtle
Turtle

Reputation: 1350

Try:

./import.py

Most people don't have "." in their path.

just typing python will call the cygwin python.

import.py will likely call whichever python is associated with .py files under windows.

You are using two different python executables.

Upvotes: 0

krawyoti
krawyoti

Reputation: 20135

I have the feeling that

C:\Temp>import.py

uses a different interpreter. Can you try with the following scripts:

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

Upvotes: 4

orithena
orithena

Reputation: 1485

Create a batch file next to your program that calls it the right way ... and I'm fairly sure you've got the problem because of an ambiguity between "windows python" (a python interpreter compiled for windows) and "cygwin python" (a python interpreter running on cygwin).

Upvotes: 0

Tuomas Pelkonen
Tuomas Pelkonen

Reputation: 7821

Probably py extension is connected to some other python interpreter than the one in /usr/bin/python

Upvotes: 1

Related Questions