cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
854
Views
11
Helpful
2
Replies

Python pdb for troubleshooting

smadappa
Level 1
Level 1

Hi,

Anyone know how to use python pdb debugger within NSO service code for debugging the code?

Regards,

Shameer

2 Replies 2

vleijon
Cisco Employee
Cisco Employee

Hi Shameer

You cannot easily use pdb with service code, because it runs as a service.  You can potentially use the pdb module to enhance your logging, but it is probably not what you want.

What you want is a debugger that can be attached remotely. I know that pycharm has a debugger like that, but I have never tried using it with nso.  This thread https://communities.cisco.com/message/249111#249111 talks a little bit about it and it seems that others have had success with it.

Best Regards,

Viktor

Well, while Viktor said - "you cannot easily use pdb with service code" - doesn't mean it's impossible, and frankly it's really not that hard at all. The functionality has been there since version NSO 4.2 when we began supporting Python for service development.

 

Try this in ncs_cli -J (configure mode):

% show python-vm | details
auto-start   true;
auto-restart true;
run-in-terminal {
    disabled;
    terminal-command "xterm -title ncs-python-vm -e";
}
logging {
    log-file-prefix ./logs/ncs-python-vm;
    level           level-info;
}

You can configure NSO to run each Python VM in a terminal! If you're okay with xterm the only thing you need to configure is:

% set python-vm run-in-terminal enabled

So, now NSO will execute the startup script in an xterm terminal and you may use

import pdb
pdb.set_trace()

in your service-callback to set a breakpoint and invoke pdb.

Please note that this will make all Python VMs controlled by NSO run in an xterm so a couple of windows may pop up depending on your configuration. If you're on a Mac you can use the xQuartz.app as an X11 server.

 

Good luck!

/Tomas