Getting started with nerd.vision in Python

Mikey

June 11, 2020

The python agent for nerd.vision has left early access, version 1.0.0 is officially released!

In this blog, we are going to guide you through setting up and using nerd.vision with your python application.

The application we will be using is the same Nerdulator app we saw in the Getting started with Node.js blog but with a Python REST endpoint.

The python app uses tornado and contains a known issue where subtractions return the wrong result.

This blog will be split into 3 parts;

  1. Installing the nerd.vision agent in our Python application
  2. Creating a workspace in nerd.vision for our Python application
  3. Debugging an example issue

How to install nerd.vision in Python

Installing the python agent requires you to do two things;

  1. Install the nerd.vision agent through PIP
  2. Initialize nerd.vision in the Python Application.

This process should only take a few minuites. You can view the video on Installing the Python agent here.

The nerd.vision agent will have no performance impact on the application, so you can leave it installed and be ready to debug your application at any time.

Installing the nerd.vision agent through PIP

To install the nerd.vision agent run:

pip install nerdvision

Depending on your configuration of PIP, you may need to run this command within the directory of your Python application.

Initializing nerd.vision in the Python Application

The agent should be initialized as soon as the application starts. For this reason we recommend placing the required code directly under the import statements of the process launch class.

The code required is as follows:

import nerdvision

nerdvision.start('YOUR_API_KEY')

The first line imports the package from your local PIP repository, the second runs nerd.vision when the application starts.

(Optional step) Tagging  and naming your clients

You can specify configuration options in the start command by placing an object in the start function, rather than just the API key.

An example of this would be:

nerdvision.start(
   api_key='YOUR_API_KEY',
   name="PythonDemo",
   tags={
       'environment':'production',
       'app':'pythonApp'
       }
   )

You can set a defined list of tags for your client. This could be tags such as  “environment=production”. A tag is made of a key-value pair and can be any combination of key/value you like.

These tags can be used to dictate if a breakpoint is fired on a particular client.

You can find the full list of configuration options for the python client here.

Confirming nerd.vision is installed

When you run the Python application after installing nerd.vision, you will see the following block of code in the terminal or log of the application:

Create a workspace in nerd.vision

In this example we will be debugging the code that was checked out from GitLab and modified locally. For this we will debug using the Local File System workspace, where the code of the application is uploaded from the source directory to the browser.

Nerd.vision does not store or process any of your code in our systems as we value your privacy.

To create your workspace, click 'Create New Workspace'

In the create workspace modal, you can then browse for your project folder and click upload

As an additional step, we will then expand additional options and filer on the tag APP = "PYTHONAPP" as shown in the Client tagging section above

Hitting save will create the workspace.

Debugging an issue with nerd.vision

To debug an issue we need to first hit a breakpoint, we can then trigger the code in our app to fire the breakpoint.

In this example we will be debugging a known issue where a subtraction returns an incorrect result.

Setting a breakpoint in the application code

Using the source code viewer you can expand the file trees in your application to view your application files, you can also search for the specific file you require.

When you select a file (in our case nerdulator.py) the file loads in the code viewer. In our case we want to set a breakpoint on the return of our subtraction function, to do this we will left click on the margin of line 40 to set a breakpoint.

Triggering a breakpoint

To trigger a breakpoint, we will perform a subtraction in our nerdulator application

When we run the subtraction code, a breakpoint will fire in nerd.vision.

Debugging using the breakpoint snapshot

When a breakpoint fires in nerd.vision a snapshot of the debugging data is created. This snapshot contains the full call stack and variables for each frame in the stack.

In our case we can see our integer variables and result variable in the top frame of the stack.

If we compare these variables to the code of the subtract function, we can see int1 and int 2 are set correctly, but the result object is incorrect.

In the code we can see that result is equal to int2 - int1 which would explain why the result variable is not correct, the values int1 and int2 should be reversed.

Our example of debugging an issue looked into a very basic issue, but the process for debugging a more complex issue is the same.

Conclusion

Setting up and debugging an issue in Python using nerd.vision is a very quick process. Setting up the python agent and worksapce only takes a few minutes and gives you the ability to debug any environment at a moments notice.

Mikey

Mikey

Technical Support Engineer and Developer advocate for nerd.vision and other software projects. Background in object-oriented software development and data analysis. Personal hobbies include simulation gaming and watch collecting.