When I started at Cisco I wrote a blog post "How to Debug NSO Services and Action with Pycharm" which remain actual even 3 years later, a customer recently said to me that he is not using Pycharm but it would be nice to have the same article with VsCode .
So here it is :
On the python which is running the NSO python package, you want to debug :
pip install debugpy
This is a Microsoft library who wraps the well-known pydevd library to make it compatible with VsCode.
We will do remote debugging, debugpy will create a debug server on the NSO side and VsCode will connect to it, then the debug server will send all debug infos to VsCode.
On this demo I will debug on a local install of NSO but the debug can be done on a distant server (don't forget to open the port)
If you click on the Run and Debug icon of VsCode you will see something like this :
![run-and-debug.png run-and-debug.png](https://community.cisco.com/t5/image/serverpage/image-id/149825iFF75F048CDBADB14/image-size/large?v=v2&px=999)
Click on "create a launch.json file" this will open the launch.json file.
Copy and paste the following section in the brackets :
{
"name": "Python: NSO Action",
"type": "python",
"request": "attach",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"port": 5678,
"host": "localhost",
}
You should see something like that :
![json-nso-action.png json-nso-action.png](https://community.cisco.com/t5/image/serverpage/image-id/149826iD62B4B848287C683/image-size/large?v=v2&px=999)
Save the file and you should have a "Python NSO action" debug setting with a run button at the top left.
![run-debug.png run-debug.png](https://community.cisco.com/t5/image/serverpage/image-id/149827iFC16902C08C4EBB1/image-size/large?v=v2&px=999)
In the main.py file, class Main, method setup(), of your NSO package put the line :
debugpy.listen(5678)
![debugpy-listen.png debugpy-listen.png](https://community.cisco.com/t5/image/serverpage/image-id/149872iB4E3FA0D2506C627/image-size/large?v=v2&px=999)
This will open the port of the debug server after a package reload or a package redeploy. Don't forget to remove it after debugging
It's also possible to change the ip address of the debug server by providing a tuple (ip_adress,port) for example:
debugpy.listen(('0.0.0.0', 5678))
Now we must put a breakpoint, I didn't manage to make the graphical red breakpoint (in screenshot below it's at line 17) it don't work I don't know why.
UPDATE feb 2023 : now we know why, as always it's a mapping problem ! It's now resolved by @atreyulovesyou in this comment below .
Another method to get breakpoint works it to use the programmatic breakpoint
debugpy.breakpoint()
In the screenshot it is at line 19.
![different-breakpoint.png different-breakpoint.png](https://community.cisco.com/t5/image/serverpage/image-id/149828i9D197FF20BB4D8AA/image-size/large?v=v2&px=999)
Ok so you can do a package reload to run the debug server and to take the breakpoint into account.
To connect to the debug server click on the green triangle.
![run-debug.png run-debug.png](https://community.cisco.com/t5/image/serverpage/image-id/149829i69D322DA3DD65D36/image-size/large?v=v2&px=999)
A list appears in the "Call Stack" section in the UI :
![call-stack.png call-stack.png](https://community.cisco.com/t5/image/serverpage/image-id/149830i959FDE43AC4C8EA8/image-size/large?v=v2&px=999)
If you launch your action, you'll see the arrow on the next instruction to be executed in the code.( you can raise the value of the timeout of the NSO service or action if you want )
UPDATE feb 2023 : with NSO if you want a lot of time to debug there is a global setting to raise: service-callback-timeout
set services global-settings service-callback-timeout 300
![debug-running.png debug-running.png](https://community.cisco.com/t5/image/serverpage/image-id/149831i34A56913914A64AC/image-size/large?v=v2&px=999)
That's it ,now you're able to debug with VsCode.
Don't forget to remove the "debugpy.listen(5678)" on your production code , the breakpoint is more easy to remember as your service/action will be stuck , but the debug server can be a big security hole if you let it run. <= Very important don't miss this.