cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
809
Views
10
Helpful
1
Replies

Integrate NSO Python with VS Code

kiran kotari
Cisco Employee
Cisco Employee

I see many articles to Integrate NSO Python with PyCharm, but not for VS Code. 

 

I am using: 

OS: Mac

NSO: Local Installation 

Python: ~/.Pyenv/versions/3.7.4 

 

Did anyone tried, can anyone please guide me on how to Integrate NSO Python with Visual Studio Code (VS Code)

1 Reply 1

sarifern
Cisco Employee
Cisco Employee

NCS PyAPI libraries in VSCode

1. Disable activation of environments under Code>Settings

disable.png

2. Create local python virtualenv through pyenv

 

SARIFERN-M-V71Y:ncs-5.2.1.1-alu $ pyenv virtualenv 3.7.12 venvNSO3.7.12            
Looking in links: /var/folders/h_/tl2jd88d6g1987kcs6xgg5dw0000gn/T/tmp_ncmgixi
Requirement already satisfied: setuptools in /Users/sarifern/.pyenv/versions/3.7.12/envs/venvNSO3.7.12/lib/python3.7/site-packages (47.1.0)
Requirement already satisfied: pip in /Users/sarifern/.pyenv/versions/3.7.12/envs/venvNSO3.7.12/lib/python3.7/site-packages (20.1.1)

 

3. Set it up as global python

 

SARIFERN-M-V71Y:ncs-5.2.1.1-alu $ pyenv global venvNSO3.7.12          
(venvNSO3.7.12) SARIFERN-M-V71Y:ncs-5.2.1.1-alu 

 

4. Create links to the NCS python libraries to have IDE suggestions.

 

(venvNSO3.7.12) SARIFERN-M-V71Y:ncs-5.2.1.1-alu $ ln -s ~/NSO/ncs-5.2.1.1/src/ncs/pyapi/* ~/.pyenv/versions/3.7.12/envs/venvNSO3.7.12/lib/python3.7/site-packages/.

 

5. Select the virtualenv as interpreter. Enter the interpreter path to the python3 interpreter in the virtualenv

By this point, the IDE should provide autocomplete and suggestions:

ncs.png

Debugging

1. With the virtualenv as your global python installation, Install debugpy

2. Create a "Remote Attach" configuration file:

remote_attach.png

3. Choose the hostname where the NSO is being executed (in this case, localhost, but it could be a remote machine)

4. Select an unused port (i.e. 8890)

5. Modify the remoteRoot variable to the packages-in-use folder of the NSO run installation and optionally, change the name of your debugger configuration:

 

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "NSO debugger",  <<<<<<
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 8890 <<<<<<<
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "~/NSO/nso-5.2.1.1-alu/state/packages-in-use/1" <<<<<<
                }
            ]
        }
    ]
}

 

6. In the file that you want to debug, place this snippet at the top (with the used port):

 

### TODO: REMOVE AFTER DEVELOPMENT
import debugpy
debugpy.listen(8890)
debugpy.wait_for_client()
### TODO: REMOVE AFTER DEVELOPMENT

7. Place an initial breakpoint where you want to begin debugging:

 

i.e.

 

with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, "admin", "python"):
        with m.start_write_trans() as t:
            root = ncs.maagic.get_root(t)
            # First step is to check-sync and then sync-from devices
            self.sync_from_if_not_check_sync(root, input.devicea)
            self.sync_from_if_not_check_sync(root, input.devicez)
            # 1. query each device for ip and LSP interface name
            debugpy.breakpoint() <<<<<

 

8. Stop and start NSO

9. Reload the packages in NSO.

 

(venvNSO3.7.12) SARIFERN-M-V71Y:ncs-5.2.1.1-alu $ source ~/NSO/ncs-5.2.1.1/ncsrc
(venvNSO3.7.12) SARIFERN-M-V71Y:ncs-5.2.1.1-alu $ ncs_cli -u admin -C
admin connected from 127.0.0.1 using console on SARIFERN-M-V71Y

admin@ncs# packages reload

 

10. Click on the play button for the debugger.

11. Happy debugging! Further breakpoints can be set through the IDE, step by step execution, etc.

 

Disclaimer

Tested in an old NSO version (5.2.1.1). I tried this exact procedure with python+=3.9.0 and it didn't work. Last version that worked was 3.8.12. Maybe in newer versions it will support 3.9+.

Sources

https://gitlab.com/nso-developer/nso-docker/-/issues/173

Local script debugging https://code.visualstudio.com/docs/python/debugging#_local-script-debugging

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community:

Recognize Your Peers