Stephen Hsu
Stephen Hsu

Reputation: 5187

How to pdb Python code with input?

I'm debugging Python code with pdb. The code need input from stdin, like:

python -m pdb foo.py < bar.in

Then the pdb will accept the bar.in as commands. How to tell pdb that the input is for foo.py and not for pdb?

Upvotes: 6

Views: 1251

Answers (2)

Peter Lyons
Peter Lyons

Reputation: 146114

Well, this is a tweak to Aaron's answer, but I think it misses the point in that you want to interactively debug at some point, right? This works but the program exits before you get a chance to debug.

(echo cont;cat bar.in) | python -m pdb foo.py

I think if you can edit foo.py, do import pdb then at the interesting point in foo.py do pdb.set_trace(), and just run python foo.py without the -m pdb and give it bar.in normally

python foo.py < bar.in

Upvotes: 3

Aaron Maenpaa
Aaron Maenpaa

Reputation: 122960

A kind of gross work around is to put cont at the beginning of bar.in:

cont
one
two
three
four


aaron@ares ~$ python -m pdb cat.py < bar.in 
> ~/cat.py(1)<module>()
-> import sys
(Pdb) one
two
three
four
The program finished and will be restarted
> ~/cat.py(1)<module>()
-> import sys
(Pdb) 

Upvotes: 1

Related Questions