Python Ubuntu 中的调试器:当您的 Python 代码中有 sys.stdin 时如何使用 PDB?

Python DeBugger in Ubuntu: How to use PDB when you have sys.stdin in your Python code?

我有以下 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”运行完全没问题

但现在我想对“map.py”Python 程序使用 Python DeBugger (pdb)。

所以我插入了行 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

请帮忙,我怎样才能 运行 Python 使用标准输入 sys.stdin 调试器?

我的意思是,在我的例子中,如何使用 pdb 调试上面的 Python 程序“map.py”?请帮忙

如果您的标准输入不可用,您可能想尝试 wdb

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

使用方法:

  • 安装:pip install wdb wdb.server
  • 在另一个终端中,启动服务器:wdb.server.py &
  • 在您的代码中,使用 wdb:
  • 而不是 pdb
    import wdb
    wdb.set_trace()

这是一张显示 wdb 正在运行的屏幕截图,取自其主页。

我过去曾在标准输入不可用的守护进程中使用 wdb。