This page describes how to configure your environment and your
Python application to use Stackdriver Debugger.
For some environments, you must explicitly specify the access scope to let the
Stackdriver Debugger agent send data.
We recommend setting the
broadest possible access scope and then using Cloud Identity and Access Management
to restrict access.
In keeping with this best practice, set the access scope to be all Cloud APIs
with the option cloud-platform.
Before you begin
To begin using Debugger, ensure the Debugger API is enabled.Enable Debugger API
App Engine standard environment
Python 2.7
If you are using Python 2.7, the Debugger is enabled by default and no configuration is required.
The Debug page in the GCP Console automatically displays the deployed files matching the app source code. To learn more about source code matching, see Selecting source code automatically.
Python 3.7
If you are using Python 3.7, you must manually enable the Debugger agents by performing the following steps:
Make sure your
app.yamlfile contains the following lines:runtime: python37Add the following lines as early as possible in your initialization code, such as in your main function, or in
manage.pywhen using the Django web framework (version 1.* only).try: import googleclouddebugger googleclouddebugger.enable() except ImportError: passAdd
google-python-cloud-debuggertorequirements.txt.To have the Debug page in the GCP Console automatically display source code matching the deployed app, go to Selecting source code automatically.
The Debugger is now ready for use with your app.
App Engine flexible environment
You can use the Debugger with the App Engine Python Runtime or Custom Runtime.
Make sure your App Engine Flexible VM instances are running:
- A 64-bit Debian Linux image
- Python 2.7 or 3.6
Make sure your
app.yamlfile contains the following lines:runtime: python env: flex # The Python runtime uses Python 3.6 by default. # To use Python 2.7 add the following lines. # runtime_config: # python_version: 2If you are using a Custom Runtime, use
runtime: custom.Add
google-python-cloud-debuggertorequirements.txt.Add the following lines as early as possible in your initialization code, such as in your main function, or in
manage.pywhen using the Django web framework (version 1.* only).try: import googleclouddebugger googleclouddebugger.enable() except ImportError: passTo have the Debug page in the GCP Console automatically display source code matching the deployed app, see Selecting source code automatically.
The Debugger is now ready for use with your app.
Google Kubernetes Engine
GCLOUD
To enable Debugger using gcloud, complete the following steps:
Create your cluster with one of the following access scopes:
https://www.googleapis.com/auth/cloud-platformgrants your cluster access to all Google Cloud Platform APIs.https://www.googleapis.com/auth/cloud_debuggergrants your cluster access to only the Debugger API. Use this access scope to harden your cluster's security.
gcloud container clusters create example-cluster-name \ --scopes=https://www.googleapis.com/auth/cloud_debuggerAdd the Debugger package to your app:
If you use a
requirements.txtfile, add the following line:google-python-cloud-debuggerIf you use a
Dockerfile, add the following line:RUN pip install google-python-cloud-debuggerAdd the following lines as early as possible in your initialization code, such as in your main function, or in
manage.pywhen using the Django web framework:try: import googleclouddebugger googleclouddebugger.enable() except ImportError: pass
On the Debug page, select the ___location of the source code. To have the Debug page in the GCP Console automatically display source code matching the deployed app, see Selecting source code automatically.
The Debugger is now ready to use.
CONSOLE
To enable Debugger using the console, complete the following steps:
After selecting your cluster type, click More options from the Node pools pane:
Select one of the following from the Security pane:
Allow full access to all Cloud APIs.
Allow access for each API and then select Enabled for Cloud Debugger.
Compute Engine
Make sure your Compute Engine VM instances are running:
- A 64-bit Debian Linux image
- Python 2.7 or 3.7
Make sure your Compute Engine VM instances are created with the access scope option Allow full access to all Cloud APIs, or have one of the following access scopes:
https://www.googleapis.com/auth/cloud-platformhttps://www.googleapis.com/auth/cloud_debugger
Download the Debugger agent.
The easiest way to install the Python Debugger is with
pip:pip install google-python-cloud-debuggerAdd the following lines as early as possible in your initialization code, such as in your main function, or in
manage.pywhen using the Django web framework.try: import googleclouddebugger googleclouddebugger.enable( module='[MODULE]', version='[VERSION]' ) except ImportError: passIf you can't change the code, run the Debugger agent as a module:
python -m googleclouddebugger \ --module=[MODULE] \ --version=[VERSION] \ -- \ myapp.pyWhere, in both cases:
[MODULE]is the name of your app.
This, along with the version, is used to identify the debug target in the GCP Console Debug page.
Examples:MyApp,Backend, orFrontend.[VERSION]is the app version (for example, the build ID).
The GCP Console Debug page displays the running version as[MODULE] - [VERSION].
Example values:v1.0,build_147, orv20170714.
The Debugger is now ready for use with your app.
To have the Debug page in the GCP Console automatically display source code matching the deployed app see Selecting source code automatically.
Cloud Run and Cloud Run on GKE
Python package.
If you use a
requirements.txtfile, add the following line:google-python-cloud-debuggerIf you do not, add the following line to your
Dockerfile:RUN pip install google-python-cloud-debuggerAdd the following lines as early as possible in your initialization code, such as in your main function, or in
manage.pywhen using the Django web framework:try: import googleclouddebugger googleclouddebugger.enable() except ImportError: pass
On the Debug page, select the ___location of the source code. To have the Debug page in the GCP Console automatically display source code matching the deployed app, see Selecting source code automatically.
The Debugger is now ready to use.
Local and elsewhere
Make sure your workstation is running:
- A 64-bit Debian Linux image
- Python 2.7 or 3.7
Download the Debugger agent.
The easiest way to install the Python Debugger is with
pip:pip install google-python-cloud-debuggerDownload service account credentials.
To use the Stackdriver Debugger agent for Python on machines not hosted by Google Cloud Platform, the agent must use GCP service-account credentials to authenticate with the Stackdriver Debugger Service.Use the GCP Console Service Accounts page to create a credentials file for an existing or new service-account. The service-account must have at least the
Stackdriver Debugger Agentrole.Place the service-account JSON file alongside the Stackdriver Debugger agent for Python.
Add the following lines as early as possible in your initialization code, such as in your main function, or in
manage.pywhen using Django Web Framework.try: import googleclouddebugger googleclouddebugger.enable( module='[MODULE]', version='[VERSION]' service_account_json_file='/opt/cdbg/gcp-svc.json') except ImportError: passIf you can't change the code, run the Debugger agent as a module:
python \ -m googleclouddebugger \ --module=[MODULE] \ --version=[VERSION] \ --service_account_json_file=/opt/cdbg/gcp-svc.json \ -- \ myapp.pyWhere, in both cases:
[MODULE]is the name of your app.
This, along with the version, is used to identify the debug target in the GCP Console Debug page.
Examples:MyApp,Backend, orFrontend.[VERSION]is the app version (for example, the build ID).
The GCP Console Debug page displays the running version as[MODULE] - [VERSION].
Example values:v1.0,build_147, orv20170714.- The
GOOGLE_APPLICATION_CREDENTIALSenvironment variable can be used instead of specifyingservice_account_json_file.
The Debugger is now ready for use with your app.
The Debug page in the GCP Console can display local source files, without upload, for local development. See Selecting source code manually.
Troubleshooting
The google-python-cloud-debugger Python package requires the python2.7-dev
Linux package and the setuptools and wheel Python packages. While these
packages are typically installed when you install pip with all suggested
dependencies, missing some of them can cause the following errors.
ImportError: No module named setuptoolsThe
setuptoolsPython package needs to be installed (for example,pip install setuptools).invalid command 'bdist_wheel'orFailed building wheel for '...'The
wheelPython package needs to be installed (for example,pip install wheel).ImportError: libpython2.7.so.1.0: cannot open shared object file: No such file or directoryThe
python2.7-devLinux package needs to be installed (for example,apt-get install python2.7-dev).