Juliette Foucaut - 14 Aug 2014 - edited 31 Dec 2014
[Update 12 Aug 2015 - The methods described below do not work in Visual Studio 2015 PTVS with GAE 1.9.17 or above. We haven't found a new workaround. We've switched to using the free PyCharm CE which works great. See our tutorial to configure it.]
For python code destined to run on Google App Engine, we needed an IDE with comprehensive local debugging abilities, i.e. breakpoints, step-by-step execution and variables watch. PyCharm Professional Edition does all this [Edit 31 Dec 2014: for PyCharm Community Edition GAE configuration, see our tutorial], but we chose instead to use a free solution from Microsoft called Python Tools for Visual Studio (a.k.a. PTVS or PyTools). [Edit 28 Nov 2014: now Microsoft Visual Studio Community]
Sadly, due to a bug, breakpoints don't get hit when debugging dev_appserver.py. After lots of searching and trial and error, we found out that an acceptable workaround is to configure Visual Studio as follows:
Once setup, using it is no different from normal debugging. The downside is that some newer GAE functionality won't work.
As a side note, if you need to use dev_appserver.py to get the latest functionality, follow the instructions on Joe's Dev Notepad's blog. The unfortunate downside of that method is you have to repeat the Attach to Process operation every time you debug (if you know how to avoid it drop us a word). It's also relatively slow to start but you can speed it up if you only include the last 3 lines of code in "pydevd_startup.py":
import ptvsd #ptvsd.settrace() equivalent ptvsd.enable_attach(secret = 'joshua') ptvsd.wait_for_attach()
Now let's go back to our workaround.
Note: we're running Microsoft Python Tools for Visual Studio version 2.0.11016.00 and Python 2.7 with Google App Engine SDK 1.9.9 on Windows 8.0. [Edit 28 Nov 2014: both workarounds still work with Microsoft Visual Studio Community 2013 Version 12.0.31101.00 Update 4 with GAE 1.9.13]
In VS, open the project containing your Python code
Select menu Project > Properties >
Select the General tab
set Startup file to: <path to old_dev_appserver.py>
Example: "C:\Program Files (x86)\Google\google_appengine\old_dev_appserver.py"
set Working Directory to: ..
Select the Debug tab
Set Launch Mode to: Standard Python Launcher
Set Script Arguments to: --port <unused port number> <project path>
Example: "--port 8800 C:\Projects\Prototype1"
Save the project.
In VS, select menu Debug > Exceptions...
Set Type to: Python Exceptions
Set Name to: google.appengine.dist.py_zipimport.ZipImportError
In the exceptions list, expand Python Exceptions. Locate the item google.appengine.dist.py_zipimport.ZipImportError and uncheck the User-unhandled checkbox
in Visual Studio, set breakpoint(s) in your python code and press Start. DO NOT use the Google App Engine launcher.
A python.exe command window opens with a warning regarding the use of a deprecated version of dev_appserver.py, the port the app is running on, and how to open the admin console.
In our example we've configured port 8800 in the project's debug properties. As a result, the web application runs at http://localhost:8800 and the admin console is available at http://localhost:8800/_ah/admin.
Open a browser and navigate to http://localhost:<debug port number>. When breakpoints are hit, focus returns to the IDE.
[Edit 28 Nov 2014: added the advanced workaround using the current dev_appserver.py and mentioned PyCharm and the new VS Community]