【发布时间】:2021-03-03 02:21:23
【问题描述】:
我有以下计算单词的 Python 程序,我将此文件命名为“map.py”:
#!/usr/bin/env python
# import sys because we need to read and write data to STDIN and STDOUT
import sys
# reading entire line from STDIN (standard input)
for line in sys.stdin:
# to remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# we are looping over the words array and printing the word
# with the count of 1 to the STDOUT
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
print ('%s\t%s' % (word, 1))
我有一个名为“input_File”的输入文件:
Hello I am GeeksforGeeks Hello I am an Intern
geeks for geeks is the best online coding platform
welcom to geeks for geeks hadoop streaming tutorial
“map.py”和“input_File”两个文件放在同一个目录下。
(顺便说一下,我用的操作系统是Ubuntu 20.04.2 LTS,我用的是Python3)
所以为了运行 Python 程序,我在上面有两个文件的目录中打开终端并输入:
cat input_File | python3 map.py
python 程序“map.py”运行良好
但现在我想将 Python 调试器 (pdb) 用于“map.py”Python 程序。
所以我插入了 import pdb; pdb.set_trace() 在“map.py” Python 程序中,如下所示:
#!/usr/bin/env python
# import sys because we need to read and write data to STDIN and STDOUT
import sys
import pdb; pdb.set_trace()
# reading entire line from STDIN (standard input)
for line in sys.stdin:
# to remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# we are looping over the words array and printing the word
# with the count of 1 to the STDOUT
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
print ('%s\t%s' % (word, 1))
我再次运行python程序(我想用pdb来调试这个python程序):
cat input_File | python3 map.py
但这是我的终端中出现的内容:
> /home/.../.../map.py(8)<module>()
-> for line in sys.stdin:
(Pdb) *** SyntaxError: invalid syntax
(Pdb) *** SyntaxError: invalid syntax
(Pdb) *** SyntaxError: invalid syntax
(Pdb)
Traceback (most recent call last):
File "map.py", line 8, in <module>
for line in sys.stdin:
File "map.py", line 8, in <module>
for line in sys.stdin:
File "/usr/lib/python3.8/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib/python3.8/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
请帮忙,在使用标准输入 sys.stdin 时如何运行 Python 调试器?
我的意思是,如何使用 pdb 调试上面的 Python 程序“map.py”?请帮忙
【问题讨论】:
-
顺便说一句,避免useless
cat的方法是改用python3 map.py <input_File