AppEngine: Step-by-Step Debugging

2019-03-14 16:29发布

While working with AppEngine locally (i.e. using dev_appserver.py), is there anyway to do a step-by-step debugging? It is a too old fashion to use logging.info() or similar functions to show the values of all the variables in the code and decide where the error is.

4条回答
beautiful°
2楼-- · 2019-03-14 16:50

PyCharm Professional Edition enables step-by-step debugging out of the box.

If you're willing to go through a few setup steps, the free version, PyCharm Community Edition, can be configured to work with Google App Engine python too. You won't get all the advantages of PyCharm Professional Edition such as deployment, but you'll be able to do step by step debugging and get code navigation and auto-completion working.

To enable debugging, edit the PyCharm Run/Debug configuration by setting:

  • Script: App Engine's dev_appserver.py
  • Script parameters: --automatic_restart=no --max_module_instances="default:1" .
  • Working directory: your base project folder (the one which contains the app.yaml file)

For more detailed instructions, explanations, and how get code completion working in aup PyCharm CE project, see http://www.enkisoftware.com/devlogpost-20141231-1-Python_Google_App_Engine_debugging_with_PyCharm_CE.html.

If you're working on Windows and you want to use PyTools (Microsoft Visual Studio Community) to debug python for GAE, see http://www.enkisoftware.com/devlogpost-20140814-1-Python_Google_App_Engine_debugging_with_PyTools.html

查看更多
走好不送
3楼-- · 2019-03-14 16:51

To expand a little bit on codeape's answer's first suggestion: Because dev_appserver.py mucks about with stdin, stdout, and stderr, a little more work is needed to set a "code breakpoint". This does the trick for me:

import sys
for attr in ('stdin', 'stdout', 'stderr'):
    setattr(sys, attr, getattr(sys, '__%s__' % attr))
import pdb
pdb.set_trace()

You'll have to run dev_appserver.py from the command line rather than via the GUI App Engine Launcher. When the pdb.set_trace() line is executed, you will be dropped into the pdb debugger at that point.

查看更多
爷、活的狠高调
4楼-- · 2019-03-14 17:01

Eclipse PyDev supports debugging and AppEngine.

http://code.google.com/appengine/articles/eclipse.html

查看更多
爷、活的狠高调
5楼-- · 2019-03-14 17:03

If the local appengine process is a normal python process you have a couple of options:

  1. In your code, place "code breakpoints": import pdb; pdb.set_trace(). Run dev_appserver.py as normal, and the python debugger will break when it reaches the line with the code.

  2. Run dev_appserver.py in pdb. From the shell: $ python -m pdb dev_appserver.py. To set a breakpoint, use the command b filename.py:linenumber. Then use the c command to continue. See http://docs.python.org/library/pdb.html#debugger-commands

See the pdb module documentation.

查看更多
登录 后发表回答