The BCE Python SDK is the official software development kit for interacting with Baidu Cloud Engine services. It provides a convenient way for Python developers to integrate their applications with various Baidu AI Cloud products and services, such as object storage (BOS), virtual private cloud (VPC), and more. The current version is 0.9.69, and releases appear to be on an as-needed basis rather than a strict cadence, with the last update on PyPI on March 29, 2026.
pip install bce-python-sdkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a client for Baidu Object Storage (BOS) using your Access Key (AK), Secret Key (SK), and a service endpoint. It shows the basic steps to configure the client and make a sample API call (listing buckets). Remember to replace 'YOUR_AK' and 'YOUR_SK' with your actual credentials, ideally from environment variables, and choose the correct endpoint for your service and region.
Migrate to Python 3.3 or higher. Ensure your environment uses a supported Python 3 version.
Always refer to the official Baidu AI Cloud documentation for the correct endpoint URL for your specific service and the region you are operating in (e.g., `bj.bcebos.com` for BOS in Beijing).
Store AK/SK securely, preferably using environment variables (as shown in quickstart), a dedicated secrets management service, or IAM roles if running on Baidu Cloud infrastructure. Never hardcode them in your application.
Install the SDK using pip: `pip install bce-python-sdk`
Catch the `BceHttpClientError` and inspect the exception object for detailed error messages, status codes, and request IDs to diagnose the underlying cause, then verify your access keys, secret keys, endpoint, and request parameters. Example:
```python
from baidubce.exception import BceHttpClientError
try:
# Your SDK API call here
pass
except BceHttpClientError as e:
print(f"API call failed: {e}")
# Further inspect e.error_code, e.status_code, e.request_id
```Review the official `bce-python-sdk` documentation for the specific client (e.g., `BosClient`) to ensure the method name and its parameters are correct. Use `dir(client_object)` in an interactive Python session to inspect available attributes and methods.
```python
from baidubce.services.bos.bos_client import BosClient
# ... client configuration ...
bos_client = BosClient(config)
# Incorrect method name, assuming 'list_bucket' should be 'list_buckets'
try:
bos_client.list_bucket()
except AttributeError as e:
print(f"Error: {e}. Check the method name.")
# Correct fix:
bos_client.list_buckets()
```Safely access dictionary keys using the `.get()` method with a default value, or check for key existence using the `in` operator, or wrap the access in a `try-except KeyError` block.
```python
response = {'status': 'success', 'data': {'item_id': '123'}}
# Using .get() method
item_name = response.get('data', {}).get('item_name', 'N/A')
print(f"Item Name: {item_name}") # Output: Item Name: N/A
# Using try-except block
try:
item_description = response['data']['item_description']
except KeyError:
item_description = 'Description not available'
print(f"Item Description: {item_description}") # Output: Item Description: Description not available
```Verify that your Access Key ID and Secret Access Key are correct and active in your Baidu AI Cloud console. Ensure the credentials have the required permissions for the specific service and API calls you are making. Also, confirm that the region endpoint is correctly configured.