Jiang Xiang
Jiang Xiang

Reputation: 3256

pdb in JupyterLab not entering interactive mode

My goal is to run some python scripts with pdb or ipdb in JupyterLab to capture my debugging history.

I first inserted set_trace() in my python script:

import torch
from IPython.core.debugger import set_trace

def train_batch(model_instance, inputs_source, labels_source, inputs_target, optimizer, args):
    inputs = torch.cat((inputs_source, inputs_target), dim=0)
    total_loss, train_stats = model_instance.get_loss(inputs, labels_source)
    total_loss[args.train_loss].backward()

    set_trace() # insert breakpoint

    optimizer.step()
    return train_stats

I then run this script in my JupyterLab:

!python ./trainer/train.py \
    --base_config ./config/sgd_vannila_0.001.yml \
    --dataset Office-Home \
    --class_num 50 \
    --src_address ./data/office-home/Art.txt \
    --tgt_address ./data/office-home/Clipart.txt \
    --name transfer.debug.rand_noise \
    --train_steps 10000 \
    --seed 2 \
    --filter_classes=0,50 \
    --eval_interval 50

The execution stops at the breakpoint, but does not provide an interactive box to prompt any ipdb instructions. The same is happening for pdb or jupyter notebook.

enter image description here


Things I have tried:


Version information:

Upvotes: 6

Views: 2338

Answers (2)

vestland
vestland

Reputation: 61194

I think the magic function %debug is what you're looking for. Try inserting the snippet below in a jupyterlab cell and run it:

def foo(a,b):
    return(a+b)
c = foo(a=1, b=str(1))

This raises a TypeError:

enter image description here

Now, if you insert a cell below, type in %debug and run it, you'll get this:

enter image description here

Now you can run any ipdb command, like h(elp):

enter image description here

Hope this h(elps)!


Edit:

OP provided the following clarification:

What I am actually looking for is a proactive way to insert break points, i.e., how to insert breakpoint even if there isn't any error.

In that case you an use from IPython.core.debugger import set_trace in combination with the ipdb command bt. Here's an example:

from IPython.core.debugger import set_trace

def foo(a,b):
    return(a+b)
set_trace()
c = foo(a=1, b=1)

This triggers the following:

enter image description here

Now, run the command bt and I hope you'll get exactly what you're looking for. I any case, I hope this answers the 'breakpoint without an error' part. I'm not going to include the entire output of running bt, since it's quite a handful. But here's the output from running ?bt to get some more help on that particular command:

enter image description here

Upvotes: 8

mooihi
mooihi

Reputation: 26

You can do it with the %run magic command.

%run ./trainer/train.py \
--base_config ./config/sgd_vannila_0.001.yml \
--dataset Office-Home \
--class_num 50 \
--src_address ./data/office-home/Art.txt \
--tgt_address ./data/office-home/Clipart.txt \
--name transfer.debug.rand_noise \
--train_steps 10000 \
--seed 2 \
--filter_classes=0,50 \
--eval_interval 50

Upvotes: 1

Related Questions