{"id":492,"library":"google-cloud-compute","title":"Google Cloud Compute Engine Client Library","description":"The `google-cloud-compute` Python client library provides programmatic access to Google Cloud Compute Engine, which delivers virtual machines running in Google's innovative data centers and worldwide fiber network. It allows developers to manage VM instances, disks, networks, and other Compute Engine resources. The library is actively maintained by Google and receives frequent updates, with the current version being 1.46.0.","status":"active","version":"1.46.0","language":"python","source_language":"en","source_url":"https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-compute","tags":["google cloud","gcp","compute engine","vm","virtual machine"],"install":[{"cmd":"pip install google-cloud-compute","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required Python version for the library.","package":"Python","optional":false},{"reason":"Core library for Google Cloud APIs, provides common functionality like authentication, retries, and pagination.","package":"google-api-core","optional":false},{"reason":"Handles authentication with Google Cloud services.","package":"google-auth","optional":false},{"reason":"gRPC client for high-performance communication with Google Cloud APIs.","package":"grpcio","optional":false}],"imports":[{"note":"The canonical import path for Compute Engine v1 API client is `google.cloud.compute_v1`. Older or incorrect patterns might refer to `google.cloud.compute` directly, which is not the recommended or stable way to access the specific API version.","wrong":"from google.cloud.compute import Client","symbol":"InstancesClient","correct":"from google.cloud import compute_v1"}],"quickstart":{"code":"import google.cloud.compute_v1 as compute_v1\nimport os\n\ndef list_all_instances(project_id: str) -> None:\n    \"\"\"Lists all instances across all zones in the given project.\"\"\"\n    client = compute_v1.InstancesClient()\n    # aggregated_list returns instances grouped by zone\n    aggregated_list = client.aggregated_list(project=project_id)\n\n    print(f\"Instances in project {project_id}:\")\n    for zone, scope in aggregated_list.items():\n        if scope.instances:\n            print(f\"  Zone: {zone}\")\n            for instance in scope.instances:\n                print(f\"    - {instance.name} ({instance.status})\")\n\n# Replace with your Google Cloud Project ID or set as an environment variable\nPROJECT_ID = os.environ.get('GCP_PROJECT_ID', 'your-gcp-project-id')\n\nif PROJECT_ID == 'your-gcp-project-id':\n    print(\"WARNING: Please set the 'GCP_PROJECT_ID' environment variable or replace 'your-gcp-project-id' in the code.\")\nelse:\n    try:\n        list_all_instances(PROJECT_ID)\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n        print(\"Ensure the Compute Engine API is enabled for your project and your credentials are set up correctly.\")\n","lang":"python","description":"This quickstart code demonstrates how to authenticate and list all virtual machine instances within a specified Google Cloud project. It uses Application Default Credentials (ADC) for authentication, which is the recommended method. Replace `your-gcp-project-id` or set the `GCP_PROJECT_ID` environment variable."},"warnings":[{"fix":"Upgrade your Python environment to Python 3.7 or newer. The library now supports Python >= 3.7, including 3.14.","message":"The `google-cloud-compute` library, along with other Google Cloud Python client libraries, has officially dropped support for Python 2.7 and Python 3.5.","severity":"breaking","affected_versions":"<=1.x.x (for Python 2.7), Check specific changelogs for Python 3.5, generally before 1.x.x or 2.x.x for some libraries."},{"fix":"Leverage Application Default Credentials (ADC). For local development, use `gcloud auth application-default login`. For production on GCP, use service accounts attached to your compute resources (e.g., VMs, Cloud Run, Cloud Functions) which ADC will automatically pick up.","message":"Relying on hardcoded API keys or service account key files for authentication in production environments is discouraged and a security risk.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Reuse a single client instance throughout your application's lifecycle, especially for frequent API interactions. Client libraries are designed to be long-lived and handle connection pooling internally.","message":"Creating a new client instance (e.g., `compute_v1.InstancesClient()`) for every API call can lead to significant performance overhead due to connection establishment.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Iterate directly over the results returned by list methods. The client library handles fetching subsequent pages transparently. If you need to access page-level metadata or stop iteration early, you can access the `.pages` attribute of the iterator.","message":"Many API list methods return paginated results. While Google Cloud client libraries automatically handle pagination using iterators, developers might incorrectly try to implement manual pagination, leading to inefficient code or missed results.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Choose either a synchronous or asynchronous client based on your application's architecture and stick to it. Asynchronous clients are generally preferred for I/O-bound operations and high concurrency (e.g., web servers, batch processing), while synchronous clients are simpler for sequential scripts.","message":"The library provides both synchronous and asynchronous clients (e.g., `InstancesClient` vs `InstancesAsyncClient`). Mixing synchronous and asynchronous patterns or choosing the wrong client for your workload (e.g., synchronous for high-concurrency I/O-bound tasks) can lead to performance bottlenecks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Increase the `idea.max.intellisense.filesize` setting in your IDE's custom properties (Help -> Edit custom properties…) to a larger value, such as `10000` (10MB).","message":"Due to the large size of `google-cloud-compute` and other Google Cloud Python libraries, code completion in JetBrains IDEs (like PyCharm) may fail.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the `GCP_PROJECT_ID` environment variable is set to your actual Google Cloud project ID. Alternatively, pass the project ID explicitly as an argument to client methods where applicable.","message":"Operations requiring a Google Cloud project ID will fail if the `GCP_PROJECT_ID` environment variable is not set or if a placeholder project ID is used in the code.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T14:17:55.418Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Grant the appropriate IAM role to the user or service account. For example, for creating VMs, grant `roles/compute.instanceAdmin.v1` or a broader role like `roles/editor`. Use `gcloud projects add-iam-binding PROJECT_ID --member='user:ACCOUNT_EMAIL' --role='ROLES/COMPUTE.INSTANCEADMIN.V1'` or manage permissions via the GCP Console.","cause":"The user account or service account used for the operation lacks the necessary IAM permissions (roles) to perform the requested action on the Compute Engine resource or within the Google Cloud project.","error":"The caller does not have permission"},{"fix":"Verify the project ID for correctness. Ensure the Compute Engine API is enabled for the project via the GCP Console or `gcloud services enable compute.googleapis.com`. Confirm the authenticated account has at least `roles/viewer` on the project.","cause":"The specified project ID is incorrect, the project does not exist, or the user/service account does not have permission to view the project, making it appear as if it doesn't exist. Sometimes, it can also mean the Compute Engine API is not enabled for that project.","error":"The resource 'projects/PROJECT_ID' was not found."},{"fix":"Enable the Compute Engine API for your project using the Google Cloud Console (APIs & Services -> Enabled APIs & services) or via the `gcloud` CLI: `gcloud services enable compute.googleapis.com --project=PROJECT_ID`.","cause":"The Google Compute Engine API, which `google-cloud-compute` relies on, has not been activated or is currently disabled for the specified Google Cloud project.","error":"Compute Engine API has not been used in project PROJECT_NUMBER before or it is disabled."},{"fix":"Set the `CLOUDSDK_PYTHON` environment variable to explicitly point to a compatible Python version (e.g., Python 3.9 or earlier) if you have one installed: `export CLOUDSDK_PYTHON=/usr/bin/python3.9` (adjust path as needed). Alternatively, ensure your `gcloud` SDK is updated to a version that supports Python 3.10+.","cause":"This error typically occurs when using the `gcloud` CLI (which some `google-cloud-compute` operations or authentication rely on) with Python 3.10 or newer. The `collections.Mapping` abstract base class was removed in Python 3.10 and moved to `collections.abc.Mapping`.","error":"AttributeError: module 'collections' has no attribute 'Mapping'"},{"fix":"Try creating the resource in a different zone. If the machine type is flexible, consider a different machine type or a custom machine type. You can also wait and retry the request at a later time, as resource availability can change. For persistent needs, request a quota increase.","cause":"Google Cloud does not have sufficient capacity for the requested Compute Engine resource (e.g., VM instance, GPU) in the specified zone at that moment. This is common for popular machine types or specialized hardware like GPUs.","error":"The zone 'projects/PROJECT_ID/zones/ZONE_NAME' does not have enough resources available to fulfill the request. Try a different zone, or try again later."}],"ecosystem":"pypi","meta_description":null,"install_score":85,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":11.46,"mem_mb":167.3,"disk_size":"122.9M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":2.73,"mem_mb":86,"disk_size":"121M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":13.69,"mem_mb":164.7,"disk_size":"135.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":5.79,"mem_mb":93.4,"disk_size":"133M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":11.13,"mem_mb":162.8,"disk_size":"126.0M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":5.96,"mem_mb":92.6,"disk_size":"124M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":10.38,"mem_mb":164.4,"disk_size":"124.4M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":5.38,"mem_mb":92.9,"disk_size":"122M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":10.86,"mem_mb":167.7,"disk_size":"124.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":3.26,"mem_mb":86.9,"disk_size":"122M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}