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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibcpy 3.10–3.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.fixFor 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.fixFor 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.fixEnsure 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.