简体   繁体   中英

AppEngine: Step-by-Step Debugging

While working with AppEngine locally (ie 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.

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.

Eclipse PyDev supports debugging and AppEngine.

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

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() 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 .

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

Yes, you can do it easily Visual Studio Code these days.

First you need to install a python module debugpy

pip install debugpy

Then install Python Extension for Visual Studio Code.

Now start the server with the following command:

cd /path/to/python/project
python -m debugpy --listen 5678 /path/to/google-cloud-sdk/platform/google_appengine/dev_appserver.py .

In Visual Studio Code Debug, create a configuration inside "lauch.json"

{
    "name": "Python: Attach",
    "type": "python",
    "request": "attach",
    "connect": {
      "host": "localhost",
      "port": 5678
    }
  }

Afterwards, you can just set the breakpoints, and click run "Python: Attach".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM