简体   繁体   English

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

[英]Issue with shebang python - import command not found

I am tring to run a simple mapper code in python, unix is not recognizing my shebang line, i searched in lot of forums and everywhere it is suggested to add sehbang line and give permission to that file. 我想在python中运行一个简单的映射器代码,unix不识别我的shebang行,我在很多论坛中搜索,并建议添加sehbang行并给予该文件的权限。 I did both, but still its not working. 我做了两个,但仍然无法正常工作。 It is working only when i add python before the file. 它只在我在文件之前添加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))

The #! #! has to be on the first line: 必须在第一行:

#!/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))

The shebang has to go into the first line of your code. shebang必须进入代码的第一行。

As it currently stands, you are creating a python statement(the triple quotes), and then importing the shebang. 就目前看来,你正在创建一个python语句(三引号),然后导入shebang。

So, the correct version will be 所以,正确的版本将是

#!/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))

Edit 编辑

You have a extra character in your shebang line, the windows File ending character. 你的shebang系列中有一个额外的角色,即Windows文件结束字符。 Remove it and you should be good to go. 删除它,你应该很高兴去。 You can check this link to see how to change the encoding in notepadd++. 您可以查看此链接以了解如何更改notepadd ++中的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM