1

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

  • As an aside, the way to avoid the [useless `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) is to instead use `python3 map.py – tripleee Mar 03 '21 at 05:53

1 Answers1

2

If your standard input is unavailable, you might want to try wdb.

wdb is a full featured web debugger based on a client-server architecture.

To use it:

  • Install: pip install wdb wdb.server
  • In another terminal, start the server: wdb.server.py &
  • In your code, instead of pdb, use wdb:
    import wdb
    wdb.set_trace()

Here is a screenshot showing wdb in operation, taken from its home page.

wdb screenshot

I have used wdb in the past with daemonized processes where standard input was not available.

Koterpillar
  • 7,883
  • 2
  • 25
  • 41
  • Thank you so much @Koterpillar ; I really appreciate; You saved my day; By the way, in my case, I have to install "wdb server" by using the command ```pip install wdb.server``` before I can start the server ```wdb.server.py &``` – TrungNhan NguyenHuu Mar 03 '21 at 10:11