Google Cloud Notebooks
The `google-cloud-notebooks` client library provides Python access to the Google Cloud AI Platform Notebooks API. This managed service offers an integrated and secure JupyterLab environment for data scientists and machine learning developers to experiment, develop, and deploy models into production. The library is currently at version 1.16.0 and is part of the broader `google-cloud-python` ecosystem, which maintains a frequent release cadence, often with monthly updates for various client libraries.
Common errors
-
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.INTERNAL details = "An internal error has occurred (…)">
cause This error typically indicates a transient issue on the Google Cloud server side, such as a hardware failure, software bug, or network problem. It's often not directly related to client-side code logic.fixImplement retry logic with exponential backoff for API calls. Consider increasing the request `timeout` parameter if the issue persists, as shown in some examples. -
ImportError: No module named 'google.cloud.notebooks_v1'
cause The `google-cloud-notebooks` library or its specific `notebooks_v1` sub-module is not installed in the active Python environment, or the environment is not correctly configured.fixEnsure the library is installed (`pip install google-cloud-notebooks`) and that you are running your code in the same Python environment where it was installed. If using virtual environments, activate it before running your script. -
DefaultCredentialsError: Could not automatically determine credentials.
cause The application cannot find valid credentials to authenticate with Google Cloud. This commonly happens when running locally without setting up Application Default Credentials or when the Notebooks API is not enabled for the project.fixRun `gcloud auth application-default login` in your terminal to set up credentials for local development. Ensure the 'Notebooks API' is enabled in your Google Cloud project for the target region via the Google Cloud Console.
Warnings
- breaking The `google-cloud-notebooks` client library requires Python 3.9 or newer. Older Python versions (3.8 and below) are not supported and will lead to compatibility issues or installation failures.
- gotcha Logs generated by this library might contain sensitive information. If you enable logging and save these logs, ensure proper access restrictions are in place. The content and level of log messages can also change in future releases without being flagged as breaking changes.
- gotcha For authentication in development environments, especially within Jupyter notebooks or Colab, avoid hardcoding or directly embedding service account keys. Prefer using Application Default Credentials (`gcloud auth application-default login`) or Colab's built-in `auth.authenticate_user()` to leverage temporary credentials and reduce security risks.
Install
-
pip install google-cloud-notebooks
Imports
- NotebookServiceClient
from google.cloud.notebooks_v1.services.notebook_service import NotebookServiceClient
- types
from google.cloud.notebooks_v1 import types
Quickstart
import os
from google.cloud.notebooks_v1.services.notebook_service import NotebookServiceClient
from google.cloud.notebooks_v1 import types
# Set your Google Cloud Project ID and desired location (e.g., 'us-central1')
project_id = os.environ.get('GCP_PROJECT_ID', 'your-gcp-project-id')
location = os.environ.get('GCP_LOCATION', 'us-central1')
def list_notebook_instances(project_id: str, location: str):
"""Lists all Notebook instances in a given project and location."""
client = NotebookServiceClient()
parent = f"projects/{project_id}/locations/{location}"
print(f"Listing Notebook instances in {parent}:")
try:
request = types.ListInstancesRequest(parent=parent)
page_result = client.list_instances(request=request)
for response in page_result:
print(f" Instance: {response.name} (State: {response.state.name})")
if not page_result:
print(" No instances found.")
except Exception as e:
print(f"Error listing instances: {e}")
print("Ensure the Notebooks API is enabled for your project and location.")
print("Also, verify that your default credentials are set up (e.g., `gcloud auth application-default login`).")
if __name__ == "__main__":
list_notebook_instances(project_id, location)