Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug a python script with arguments from terminal

I have a python script that takes input arguments and runs in response to the following command in terminal (bash, Mac OSX).

python test.py arg1 arg2

Is there a good way to run the same script in debug mode without editing the code to include import pdb and pdb.set_trace()?

For example, if I'm using iPython console, I can do this by the following:

%run -d test.py arg1 arg2

This is pretty straightforward, isn't it? To achieve the same thing in terminal, I thought the following might work, but it did not:

python -c "import pdb; import sys; sys.argv = ['test.py', arg1, arg2];pdb.run('test.py')"

The code ran with the arguments, but not in pdb's debugging mode. Is it just hard to do and I should stick with pdb.set_trace or iPython's %run -d?

like image 874
Kouichi C. Nakamura Avatar asked Apr 10 '15 12:04

Kouichi C. Nakamura


1 Answers

Try:

python -m pdb test.py arg1 arg2

Running python -m pdb runs pdb as a script. If test.py is somewhere in your path rather than your current working directory, this can be a helpful substitute:

python -m pdb "$(which test.py)" arg1 arg2
like image 160
Michael Hoffman Avatar answered Sep 30 '22 14:09

Michael Hoffman