【问题标题】:Python DeBugger in Ubuntu: How to use PDB when you have sys.stdin in your Python code?Ubuntu 中的 Python 调试器:当 Python 代码中有 sys.stdin 时如何使用 PDB?
【发布时间】: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 &lt;input_File

标签: python ubuntu pdb


【解决方案1】:

如果您的标准输入不可用,您不妨试试wdb

wdb 是一个基于客户端-服务器架构的全功能 Web 调试器。

使用它:

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

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

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

【讨论】:

  • 非常感谢@Koterpillar;我真的很感激;你拯救了我的一天;顺便说一句,就我而言,我必须先使用命令pip install wdb.server 安装“wdb 服务器”,然后才能启动服务器wdb.server.py &amp;
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
相关资源
最近更新 更多