Use pdb.set_trace() in a script that reads stdin via a pipe

Here’s an example of what worked for me:

import sys
import pdb

lines = sys.stdin.readlines()
sys.stdin = open("/dev/tty")
pdb.set_trace()

Edit: since 3.7 you no longer need to import pdb for set_trace, it is available as breakpoint, so the above only requires a sys import.

import sys

lines = sys.stdin.readlines()
sys.stdin = open("/dev/tty")
breakpoint()

You may wish to replace sys.stdin.readlines() above with the iterable fileinput.input() (which defaults to sys.stdin if the list of files in sys.argv[1:] is empty) like so:

import fileinput
import sys

lines = fileinput.input()
sys.stdin = open("/dev/tty")
breakpoint()

Leave a Comment