gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:05:59 25 4
gpt4 key购买 nike

我想在 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))

最佳答案

#!必须在第一行:

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

关于python - shebang python 问题 - 找不到导入命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25863671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com