I have the following Python program that counts the words, I name this file "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))
And I have the input file named "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
Two files "map.py" and "input_File" are placed in the same directory.
(By the way, the operating system I am using is Ubuntu 20.04.2 LTS, and I am using Python3)
So in order to run the Python program, I open the Terminal in the directory that has two files above and type:
cat input_File | python3 map.py
The python program "map.py" runs perfectly fine
But now I want to use Python DeBugger (pdb) for "map.py" Python program.
So I inserted the line import pdb; pdb.set_trace() in the "map.py" Python program like below:
#!/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))
I run the python program again (I want to use pdb to debug this python program):
cat input_File | python3 map.py
But here is what appeared in my terminal:
> /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
Please help, how can I run Python Debugger when using Standard Input sys.stdin ?
I mean in my case, How can I use pdb to debug the Python program "map.py" above ? Please Help