etcd-sdk-python Client
etcd-sdk-python is a Python client for the etcd v3 API, supporting Python versions >= 3.8. It provides a simple interface for interacting with etcd, a distributed reliable key-value store. The library is actively maintained with minor releases focusing on enhancements and dependency updates.
Common errors
-
ModuleNotFoundError: No module named 'etcd_sdk_python'
cause Incorrect import path. The PyPI package name `etcd-sdk-python` does not match the importable package name `etcd_sdk`.fixChange your import statement from `from etcd_sdk_python import Client` to `from etcd_sdk import Client`. -
ModuleNotFoundError: No module named 'etcd'
cause Attempting to import the old `python-etcd` library, which is a different package for the etcd v2 API.fixInstall `etcd-sdk-python` and use `from etcd_sdk import Client`. If you need etcd v2 support, install `python-etcd` instead, but note it's deprecated. -
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC ...> Caused by RPC failure for 'etcdserver.KV/Range'
cause This error typically indicates that the etcd server is not running, is unreachable, or the connection parameters (host/port) are incorrect. It could also indicate a firewall issue.fixVerify that your etcd server is running and accessible from the client machine on the specified host and port (default 2379). Check firewall rules. Ensure etcd server is healthy. -
AttributeError: 'Client' object has no attribute 'read'
cause You are attempting to use a method (`read`) that was part of the etcd v2 API client (e.g., `python-etcd`), but `etcd-sdk-python` implements the etcd v3 API.fixUse the etcd v3 API methods like `client.get()` for reading. Consult the `etcd-sdk-python` documentation for the correct v3 API equivalents.
Warnings
- gotcha The import path for the library is `etcd_sdk` (with an underscore) despite the PyPI package name being `etcd-sdk-python` (with a hyphen). Using `from etcd_sdk_python import Client` will result in a `ModuleNotFoundError`.
- breaking This library is designed exclusively for the etcd v3 API. Attempting to connect to an etcd v2 server or use v2 API methods (e.g., `client.read()`, `client.write()`) will result in connection failures or `AttributeError`.
- gotcha Potential `grpcio` version conflicts. The library specifies a compatible `grpcio` version range, but conflicts can arise if other dependencies require a different, incompatible version.
- gotcha For robust applications, proper connection management and error handling are crucial. `etcd-sdk-python` uses gRPC, and network issues or server unavailability can lead to `_InactiveRpcError` or `grpc.RpcError`.
Install
-
pip install etcd-sdk-python
Imports
- Client
from etcd_sdk_python import Client
from etcd_sdk import Client
- Client
import etcd
from etcd_sdk import Client
- Client
import etcd3
from etcd_sdk import Client
Quickstart
from etcd_sdk import Client
import os
# Configure etcd connection (use environment variables for production)
host = os.environ.get('ETCD_HOST', '127.0.0.1')
port = int(os.environ.get('ETCD_PORT', 2379))
# Establish connection
try:
client = Client(host=host, port=port)
print(f"Connected to etcd at {host}:{port}")
# Put a key-value pair
key = 'mykey'
value = 'myvalue'
client.put(key=key, value=value)
print(f"Put '{key}': '{value}'")
# Get a key-value pair
retrieved_value, meta = client.get(key=key)
print(f"Get '{key}': value='{retrieved_value}', revision={meta.revision}")
# Delete a key
client.delete(key=key)
print(f"Deleted '{key}'")
# Try to get after deletion
deleted_value, deleted_meta = client.get(key=key)
print(f"Get '{key}' after deletion: value='{deleted_value}', revision={deleted_meta.revision}") # Value will be empty/None
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure an etcd server is running at the specified host and port.")