Debugging Python on your Pi from Visual Studio

COVER_IMAGE When I heard that the Python Tools for Visual Studio could do cross platform debugging my thoughts went immediately to the Raspberry Pi. Would they work there? Yes, they will. Amazingly easily too. It really is as simple now as following the cross platform debugging instructions.

It's a little unsatisfying to say that so let's debug something that blinks. This isn't intended to walk you through everything from scratch, but there should be enough references here to get you going. If you've already got something running and just want to add debug to it, the above guide and a read through of this should get you going.

Setting up your Pi

There are plenty of guides out there for getting your Raspberry Pi ready to use. I recommend the Easy SD Card Setup. Grab a current version of Raspbian (or your favorite distro), setup your card, plug it into the Pi, plug it into your network and power it on. There are many ways to proceed from here including using a keyboard and monitor, but I prefer finding the Pi's IP address on my router then SSH into it using Putty. Next you'll need to setup Putty to talk to your Pi, and configure your Pi for remote access from Windows. I'd also recommend installing RDP on your Pi so you can use Remote Desktop with it. Not relevant for this guide, but if you have any Macs in your house this guide will cover what you need for them.

Now, you are going to need to get pip to install Python packages. From your terminal on the Pi that's as simple as:
sudo apt-get install python-pip

Now you can install the PTVS debugger on your Pi
sudo pip install ptvsd==2.0.0

Setting up your Workstation

If you don't have Visual Studio or the Python Tools installed, go do that first. The free Visual Studio versions of Web Express or Desktop Express are fine. If you are new to Python you probably need to go grab that too. You are probably going to want the production version. 3.x is fine, but make sure to at least get 2.7.6. The Python Tools will detect any version of Python once it is installed.

The Pi uses version 2.7.6 by default, you can check that by typing python --version in a terminal on your Pi. If you want to use the later version use python3 instead of python when executing your python programs.

Debugging a Python app on the Pi

As mentioned above this is as simple as following the cross platform debugging instructions. This guide for using the GPIO pins with Python is a good place to start if you need a project, excellent instructions I'm not going to try to duplicate here. Note that instead of setting up the RPi GPIO library from source as described there you can also just use apt-get.
sudo apt-get install python-rpi.gpio

I wired up a breadboard as shown and connected it to my Pi. Then I copied the code from that guide into a VS Python project with a file called and copied it onto my Pi. Since I followed the networking with Windows guide mentioned above I'm able to connect to the Pi in Explorer, then just drag and drop my Python file into the Pi user's home directory.

To execute it from the terminal on the Pi in the home directory run
sudo python

For me this ran but rather perfectly gave me something to try with the debugger. When I started the app all the LEDs were lit, but I had expected them to be dim. I could just go flip the arguments for GPIO.HIGH and GPIO.LOW, but instead let's look around. Modify as described in the cross platform debugging instructions to import ptvsd, copy it back over, and start the app again. Now, in VS set a breakpoint inside the if condition when the reset button on GPIO 18 is pressed. I chose this location as it is setting the pins the same as on program initialization.

At this point, following the guide, I attached to the remote process and validated I could hit a break point. When the reset button is pressed I can now step through the code. Observing the LED on 11 turn on with the GPIO.LOW value I opened up the Locals window and expanded GPIO. From there I changed the value of LOW to match HIGH. Continuing too step through the code the subsequent two lines now kept the LEDs off.

Simplistic? Easy to figure without the debugger? Yes to both, but this still serves as a nice demonstration of how well these tools work with the Pi. This is going to make a lot of stuff easier, and Python my language of choice on this device.