问题与shebang python - 找不到导入命令

ds_*_*ser 2 python python-2.7

我想在python中运行一个简单的映射器代码,unix不识别我的shebang行,我在很多论坛中搜索,并建议添加sehbang行并给予该文件的权限.我做了两个,但仍然无法正常工作.它只在我在文件之前添加python时才有效.

hduser@master:~/code$ ls
mapper.py  reducer.py
hduser@master:~/code$ ls -l
total 8
-rwxrwxr-x 1 hduser hduser 225 Sep 16 07:57 mapper.py
-rw-rw-r-- 1 hduser hduser 663 Sep 16 07:56 reducer.py
hduser@master:~/code$ echo "foo foo quux labs foo bar quux" | python /home/hduser/code/mapper.py
foo     1
foo     1
quux    1
labs    1
foo     1
bar     1
quux    1
hduser@master:~/code$ which python
/usr/bin/python
hduser@master:~/code$ echo "foo foo quux labs foo bar quux" | /home/hduser/code/mapper.py
/home/hduser/code/mapper.py: line 5:
Created on 16/09/2014

@author: jee
: No such file or directory
/home/hduser/code/mapper.py: line 7: $'\r': command not found
/home/hduser/code/mapper.py: line 8: import: command not found
/home/hduser/code/mapper.py: line 9: $'\r': command not found
/home/hduser/code/mapper.py: line 11: syntax error near unexpected token `line'
'home/hduser/code/mapper.py: line 11: `    line = line.strip()
hduser@master:~/code$ vim mapper.py
'''
Created on 16/09/2014

@author: jee
'''
#!/usr/bin/python

import sys

for line in sys.stdin:
    line = line.strip()
    words = line.split()
    for word in words:
        print('%s\t%s' % (word, 1))
Run Code Online (Sandbox Code Playgroud)

Ton*_* 66 5

#!必须在第一行:

#!/usr/bin/python
'''
Created on 16/09/2014

@author: jee
'''

import sys

for line in sys.stdin:
     line = line.strip()
     words = line.split()
     for word in words:
         print('%s\t%s' % (word, 1))
Run Code Online (Sandbox Code Playgroud)