Registry / gcp / google-compute-engine

google-compute-engine

JSON →
library2.8.13pypypiunverified

The `google-compute-engine` library provides core utilities, scripts, and daemons that run within Google Compute Engine (GCE) virtual machine instances. It enables VMs to properly interact with the GCE platform, primarily by reading from and writing to the metadata server. This package is part of the standard Google-provided OS images for Compute Engine, and its primary role is to facilitate the VM's lifecycle and configuration, rather than acting as a client library for managing GCE resources externally. The latest PyPI version is 2.8.13, but the public releases on GitHub are quite old (late 2019), indicating an infrequent, system-level release cadence.

pip install google-compute-engine
INSTALL
IMPORT
SIG · GOOGLE-COMPUTE-ENG
G
google-compute-engine
gcppythonv2.8.13
Install
3.5s avg
Import
Disk
33MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.8.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.5s · import 0.000s · 31MB
33MB installed
● package 33MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

metadata_server
import google_compute_engine.metadata_server
from google_compute_engine import metadata_server

This quickstart demonstrates how to programmatically retrieve VM and project metadata from within a Google Compute Engine instance using Python's `requests` library. This is the primary way applications and system scripts on a GCE VM interact with the environment, often leveraged by the underlying `google-compute-engine` guest environment components. The metadata server provides crucial information like instance name, project ID, zone, and custom metadata, which are essential for applications to adapt to their running environment. This code should be run directly on a GCE VM.

import requests import json import os # This quickstart demonstrates how to access VM metadata from within a Google Compute Engine instance. # The google-compute-engine package helps manage this interaction at a system level. METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/' HEADERS = {'Metadata-Flavor': 'Google'} def get_metadata(path): try: response = requests.get(METADATA_URL + path, headers=HEADERS, timeout=1) response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx) return response.text except requests.exceptions.RequestException as e: # Check if running on a GCE instance or if URL is accessible if 'metadata.google.internal' in str(e): return f"Error accessing metadata. This code must run inside a Google Compute Engine VM. Details: {e}" else: return f"Error accessing metadata: {e}" if __name__ == '__main__': print('--- Instance Name ---') instance_name = get_metadata('instance/name') print(f'Instance Name: {instance_name}') print('\n--- Project ID ---') project_id = get_metadata('project/project-id') print(f'Project ID: {project_id}') print('\n--- Zone ---') zone_full = get_metadata('instance/zone') zone = zone_full.split('/')[-1] if 'zones/' in zone_full else zone_full print(f'Zone: {zone}') print('\n--- Custom Metadata (example) ---') # To test custom metadata, add a key-value pair 'my-custom-key: my-custom-value' # to your VM or project metadata via gcloud CLI or Cloud Console. # Example: gcloud compute instances add-metadata my-instance --metadata=my-custom-key=my-custom-value --zone=us-central1-a custom_key = 'instance/attributes/my-custom-key' custom_value = get_metadata(custom_key) print(f'Custom Metadata (my-custom-key): {custom_value}') print('\n--- All Project Attributes (JSON format) ---') all_project_attributes_json = get_metadata('project/attributes/?recursive=true') try: print(json.dumps(json.loads(all_project_attributes_json), indent=2)) except json.JSONDecodeError: print(all_project_attributes_json)
Debug
Known issues
gotchaThis library (`google-compute-engine`) is distinct from the Google Cloud Client Library for Compute Engine (`google-cloud-compute`). This package provides low-level guest environment utilities for VMs, while `google-cloud-compute` is the idiomatic Python client to interact with the Compute Engine API for managing resources from an external application.
fix
For managing Google Compute Engine resources (e.g., creating VMs, disks, networks) from application code, use `pip install google-cloud-compute` and import `from google.cloud import compute_v1`. Do not attempt to use `google-compute-engine` for this purpose.
affects: All versions
deprecatedThe public GitHub releases for `google-compute-engine` have not been updated since late 2019 (v20191210). While the package remains functional and is included in GCE images, significant active development or new Python-facing features should not be expected. Users should rely on `google-cloud-compute` for modern API interactions.
fix
For new application development involving Google Compute Engine, prioritize the use of `google-cloud-compute` which is actively maintained and provides current API access.
affects: 2.x.x and earlier
gotchaMetadata access relies on the HTTP metadata server (e.g., `http://metadata.google.internal`). This server is only accessible from within a Compute Engine VM instance. Attempting to query the metadata server from outside a GCE VM will result in connection errors.
fix
Ensure that any code attempting to access `metadata.google.internal` is executed on a running Google Compute Engine virtual machine instance. For external access to GCE resources, use the `google-cloud-compute` library and appropriate authentication.
affects: All versions
Upgrade
Version history
2.8.13latest on PyPI · released Jan 24, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
40 hits · last 30 days
node
36
OpenAI (training)
1
Resources
google-compute-engine — pip install google-compute-engine · libregistry