Py 01 – Visual Studio: Publish Python Script on a Unix Machine & Remote Debug


Overview

    In this post we will mention:

  • Develop Python application in a Microsoft Windows environment with Visual Studio
  • Publish it to a RaspberryPi (RPi) that is running Raspbian OS
  • Run the app on RaspberryPi and remotely debug it with Visual Studio on Windows

In this link you can find some of the answers to the question "Why to develop Python app in Visual Studio?" We will use fresh Raspbian installation and show step by step preparation of the Raspberry Pi device.

Requirements

Preparation

  • Install Visual Studio then Python tools for Visual Studio (If not already installed), connect the machine to internet
  • Install Putty on your desktop (actually just copy the single EXE file on to your Desktop)
  • Install Raspbian on RaspberryPi (setup guide)
  • Connect RaspberryPi to Internet (wired or wireless). (Howto wireless connection)

Be sure that both the development machine (with Visual Studio) and the RaspberryPi connected to the same network. You should be able to ping RaspberryPi from the windows machine.

Get IP Address

We need to know the IP address of the RaspberryPI device so we can deploy our Python script and debug it remotely. To get the IP address of the RPi device follow the steps mentioned here. In this post our RPi device's IP address is: 192.168.0.107

Get Putty & Open a Terminal Window

We don't want to switch between one monitor or keyboard to another. So we will use the development machine to access the RPi device remotely. To do so open a Putty window and enter your RPi's IP address as shown in the below screenshot. Keep the rest of the settings as they are.

    Click the "Open" button to open a terminal window to the RPi device.

Prepare RPi for Python Project

  • Login to the RPi device through the terminal window opened in the previous step.

    Default username is: pi

    Default password is: raspberry

     

     

  • Create folder named "pythonapp" and change its access mode to "0777" so anyone can access/read/write in this folder. Note its path by typing "pwd" command. In our case the path is: "/home/pi/pythonapp"

     

    Commands used in order:

        mkdir pythonapp

        chmod 0777 pythonapp

        cd pythonapp

        pwd

     

  • Update all packages to its recent version by typing the following command in the terminal window:

    sudo apt-get update

     

  • Install opensource ptvsd remote debugging libraries on RPi device by typing the following command in the terminal window:

    sudo pip install ptvsd

     

Enable Windows file share on RPi device

  • Install Samba software package by typing the following command in the terminal window:

    sudo apt-get install samba samba-common-bin

     

  • Configure Samba installation by typing the following command in the terminal window:

    sudo nano /etc/samba/smb.conf

     

    above command will open an editor with samba configuration file. Scroll down and add the following lines to the end of the configuration file.

     

     

    lines added to the end of the configuration file:

    [pythonapp]

    path=/home/pi/pythonapp

    browsable=yes

    writable=yes

    only guest=no

    create mask=0777

    directory mask=0777

    public=yes

     

    Switch back to windows, open Windows File Explorer in the development machine. Type "\\192.168.0.107\pythonapp" in the address bar. You should be able to explore the empty folder in RPi device from a windows machine.

     

     

Develop & Publish Python App

  • Open Visual Studio, create a new project.

     

  • Select the "Python Application" from the templates, and create a blank project.

 

  • Change project's Python environment by right click on the "Python Environment" node under the project name in the solution explorer window. Click on the "Add/Remove Python Environments…" item in the pop-up menu.

     

  • Select just the "Python 2.7" in the pop-up window and click OK button.

     

  • Right click on the "Python 2.7" node under the "Python Environments" in the solution explorer window. Click on the "Install Python Package…" item in the pop-up menu.

     

  • Type "ptvsd" in the package to install edit box and click OK.

     

  • Enter the following lines of Python code in to the "pythonapp.py" file through the editor.

    import
    ptvsd

    ptvsd.enable_attach(secret='my_secret')

     

    first_num = int(raw_input('Enter first number: '))

    second_num = int(raw_input('Enter second number: '))

    sum = first_num + second_num

    print('{0} + {1} = {2}: '.format(first_num, second_num, sum))

     

     

     

  • Click the "Start" button on Visual Studio toolbar to run the script locally.

     

  • Console window will open and waits you to enter two integer value, then shows the sum of these two numbers.

  • Now lets make required settings to be able publish the app to RaspberryPi device. Open the project properties window by right clicking on the project name under solution explorer window and then click on the "properties" menu item.

     

  • In the properties window, switch to the "Publish" tab, enter publish location as "\\192.168.0.107\pythonapp" (or whatever in your case) then press "Publish Now" button.

     

     

    This will copy the latest version of your Python script into the Raspberry Pi device.

     

  • Any time you update the Python script, just right click on the Project name in the solution explorer window and click on the "Publish" button afterwards. This will also copy the latest version of your Python script into the Raspberry Pi device.

Remotely Debug the Python script that is running on Raspberry Pi Device

  • Switch back to the Putty terminal window. If session timed-out, re-enter your credentials and go to the "pythonapp" folder. Type the following command in the SSH terminal window:

    python pythonapp.py

     

  • Previous command will run the python script. Before entering first number switch back to "Visual Studio" window.

  • In Visual Studio, put a breakpoint on the line that starts with "sum = first…"

     

  • In the "Debug" menu, click on "Attach to Process…" menu item.

     

  • In the "Attach to Process" window, select "Python remote (ptvsd)" as Transport. Enter "tcp://my_secret@192.168.0.107" as Qualifier and press ENTER. Available Python processes on the RPi device will be listed. Select the one to Debug and press "Attach" button in this window.

     

     

    Here the keyword "my_secret" in the Qualifier must be the same keyword that we entered in the second line of the Python source code.

     

  • Now go back to the SSH terminal to the RPi device and enter the two integer value to calculate their sum. When you enter the second number and press ENTER, Visual studio will flash and the execution cursor will stop on the breakpoint. You can browse local variables, threads, their values etc. on the Locals, Watch, Threads, Call Stack etc. windows. You can execute the script line by line remotely.

     


 

 

 

 

 

 

 

 

 

Comments (0)

Skip to main content