我正在运行Cygwin Python版本2.5.2。

我有一个三行的源文件,名为import.py:

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


当我执行“ python import.py”时,它起作用:

C:\Temp>python import.py
Success!


当我运行python解释器并键入命令时,它可以工作:

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


但是当我执行“ import.py”时,它不起作用:

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


当我删除第一行(#!/ usr / bin / python)时,我得到了相同的错误。不过,在此脚本在Linux上运行时,我需要在那一行。而且它在Linux上也能正常工作。

有任何想法吗?

谢谢。

最佳答案

我有种感觉

C:\Temp>import.py


使用不同的解释器。您可以尝试以下脚本吗:

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

09-30 18:15
查看更多