Friday, July 11, 2014

Remote debug Google App Engine development server with Python Tools for Visual Studio 2013

I mainly post this, so I will remember the next time I try it. Also because I couldn't find a complete solution anywhere else.

What I tried to do: 
After installing the Google Cloud SDK and running I wanted to debug my simple test application so I could then start to write more complicated code. For coding i use Visual Studio 2013 Pro and Python Tools for Visual Studio

I quickly ran into some trouble because to debug GAE with ptvs is not supported by ptvs.

But with a little help of some google-fu and a whole afternoon I got it running, like this:

Create A File to Inject remote debugger

1a. Get ptvsd from Microsoft Visual Studio Python Tools, if you dont have it already.
1. make a new python file ""
import json 
import sys 
if ':' not in config.version_id:  
  # The default server version_id does not contain ':'  
  import ptvsd  #ptvsd.settrace() equivalent  
  ptvsd.enable_attach(secret = 'joshua')  
3. Save it in your working directory of your app
4. for more info look at the pytool remote debuging docu I mentioned above

Edit Project Settings in VS 2013

Now open your Project Settings in VS and choose either a) or b)
A. Works with gcloud preview app (newer, autorestarts after changes)
Launch Mode->Standard Python Launcher
General->Startup File:   
C:\Program Files\Google\Cloud SDK\google-cloud-sdk\bin\..\./lib\googlecloudsdk\gcloud\
General->Working Directory:  
Debug->Search Paths:
C:\{path-to}\Cloud SDK\google-cloud-sdk\lib
Debug->Script Arguments: 
preview app run . --python-startup-script "{path-to}\" --max-module-instances="default:1"
B. Works with (older, but more reliable?)
Launch Mode->Standard Python Launcher
General->Startup File: C:\{path-to}\Cloud SDK\google-cloud-sdk\bin\
General->Working Directory: .
Debug->Search Paths: C:\{path-to}\Cloud SDK\google-cloud-sdk\lib
Debug->Script Arguments:  --python_startup_script="{path-to-your, e.g. "."}\" --automatic_restart=no --max_module_instances="default:1" "{path-to-your, e.g. "."}\app.yaml"
You could probaly also use . instead of <path-to-your-app> but I wanted to be safe.

Run Debugger

With <Ctrl+F5> you run the debugger, without debuging. This sound weird, but we are actually not debugging right now, just running the dev server which than starts our script to inject the debugger code and wait for our remote debugger to connect, which will happen in the next step

Start Remote Debugger

DEBUG->Attach to Process <Crtl+Alt+P>
Qualifier: tcp://joshua@localhost:5678 <ENTER>

joshuha is your secret key. If you want to change it (and you should), you also have to change it in the See pytool reference for more info.

But apart of this security concerns your next step should be:

Be really happy!

You now can remote debug your application locally (erm, weird). To test this you probably should use a breakpoint in your own script. I choose the
return "hello world!" 
line for this.

If you have any question, please ask. In the end it seems really simple, but to get this going was rough. Especially because pytools said, they don't support it. Microsoft probably doesn't want people to use GAE with their own Azure Cloud in the running

Start Debugging for real!

Open http://localhost:8080 in a browser (or any other address you configure your app to use). Now it should invoke the breaking point. If you are done and reload the site, it starts all over again. If you really want to end debugging or change some code, you have to restart the server and attach again. don't forget to close the terminal window with the server open (use <Crtl+C> )
also made a StackOverflow answer here


  1. Hey I had a quick question on line 6 in the second tidbit of code, what does it mean? What are the brackets used for. I think Im really close to getting this to work Im just confused as to what that is

    1. This comment has been removed by the author.

    2. Sry that just garbled somehow. You can ignore line 6. Did you get it to work?