This Quickstart shows how to use the Stackdriver Debugger to inspect the state of a simple Python app running on App Engine Standard Environment. It demonstrates how to capture the call stack and local variables using debug snapshots and how to inject logging into the running app using debug logpoints.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud Platform project. Learn how to enable billing.

  4. Make sure that the following software is installed on your local system:

Deploy to App Engine

Start by deploying a Python 2.7 app to App Engine Standard.

  1. Clone the project to a local repository:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples
  2. Go to the directory that contains the sample code:

    cd python-docs-samples/appengine/standard/hello_world
  3. Initialize the Google Cloud SDK with your project ID:

    gcloud config set project [YOUR_PROJECT_ID]
  4. Deploy your application:

    gcloud app deploy --version=v1
  5. Try your app by sending it a request:

    gcloud app browse

And the response should contain a Hello World message. If you have any issues in these steps, refer to the Quickstart for Python App Engine Standard Environment.

View Deployed Sources

Once you deploy your Python app, you can view the deployed sources on the Google Cloud Platform Console Debug page.

  1. Navigate to the Debug page in the Google Cloud Platform Console.

  2. Make sure you have the correct project selected.

  3. Make sure default - v1 (100%) is selected in the menu at the top. This indicates which service/version of your app you will be debugging.

  4. Make sure Deployed Files is selected in the menu at the left. This panel displays the source code of the Python app you just deployed.

  5. Select main.py from the list of deployed files to see its contents.

You are now ready to take debug snapshots and inject debug logpoints.

Take a Debug Snapshot

A debug snapshot captures the local variables and call stack at a specific line location in your source code. Select the Snapshot tab in the panel on the right to take debug snapshots.

  1. Click the line number for the first line inside the get function in the MainPage class. A blue arrow appears, indicating that a snapshot location is set. The results panel at the right displays "Waiting for snapshot to hit".

    The user interface with a snapshot location set on line 20.

  2. Select the Snapshot History panel at the bottom of the page to see that your snapshot has been set successfully:

    The user interface showing that a
 snapshot has been set.

  3. Send a request to your app to trigger your snapshot:

    gcloud app browse
  4. The results panel displays the call stack and the values of local variables in scope at that point in the code and at that point in time.

    The results panel showing the values of the local variables
after the snapshot was taken.

  5. Add the following statement in the Expressions panel on the Snapshot tab:


    Then click the camera icon to retake a snapshot. The following message appears under the Expressions panel: "Waiting for snapshot to hit. The running application will not stop."

    The snapshot panel with an expression entered into the expression field

  6. Send another request to trigger the snapshot again:

    gcloud app browse
  7. The Variables panel now includes the result of your expression. In this case, the value is:

    Mozilla/5.0 (X11; Linux x86_64)
    AppleWebKit/537.36 (KHTML, like Gecko)
    Chrome/74.0.3729 Safari/537.36

Inject a Debug Logpoint

A debug logpoint enables you to inject logging into your running app without restarting it. Select the Logpoint tab in the panel on the right to inject debug logpoints.

  1. Click the line number for the first line inside the get function in the MainPage class.

  2. An inline text box appears. To inject a logpoint, populate it as follows and hit Enter:

    if (True) logpoint("Browser = {self.request.environ['HTTP_USER_AGENT']}")

    The debug panel showing a logpoint injected into the source code.

  3. Select the Logpoint History panel at the bottom of the page to see that your logpoint has been injected successfully.

    The logpoint history panel showing that a
logpoint has been set.

  4. Send a request to your app to trigger the logpoint:

    gcloud app browse
  5. Select the Logs panel at the bottom of the page to see the logs printed by your logpoint.

    The logs panel showing the logs output from
 line 20.

Clean up

To avoid incurring charges to your GCP account for the resources used in this quickstart:

  1. Navigate to the Projects page of the GCP Console.

  2. Click Delete next to your project.

  3. Type your project name to confirm the shutdown.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Debugger Documentation