Alibaba Cloud Tea for Python
Alibaba Cloud Tea for Python is the core `tea` module of the Alibaba Cloud Python SDK ecosystem. It acts as a low-level library primarily designed to support Darabonba OpenAPI DSL, facilitating HTTP requests and foundational utilities for other higher-level Alibaba Cloud SDKs. Its current version is 0.4.3, and it receives frequent updates, often tied to releases of dependent SDK components.
Warnings
- breaking Starting from version 0.4.0, `alibabacloud-tea` requires Python 3.7 or higher. Older Python versions will not be supported.
- gotcha Encountering `ModuleNotFoundError: No module named 'Tea'` typically indicates an outdated `pip` version or an incorrect package installation. Users might mistakenly install a package named `tea` instead of `alibabacloud-tea`.
- gotcha When using Alibaba Cloud SDKs that rely on `alibabacloud-tea`, always handle `TeaException` and `TeaUnretryableException` for robust error management, differentiating between business logic errors and network issues.
- gotcha Avoid hardcoding Alibaba Cloud AccessKey ID and Secret. This is a security risk and generally not recommended for production environments.
Install
-
pip install alibabacloud-tea
Imports
- TeaModel
from Tea.model import TeaModel
- TeaCore
from Tea.core import TeaCore
- TeaException
from Tea.exceptions import TeaException
- TeaUnretryableException
from Tea.exceptions import TeaUnretryableException
Quickstart
import os
from Tea.model import TeaModel
from Tea.exceptions import TeaException
class MyRequest(TeaModel):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.my_param = kwargs.get('my_param', None)
def validate(self):
# Example validation: my_param is required
if self.my_param is None:
raise TeaException({'code': 'MissingParameter', 'message': 'my_param is required'})
def main():
print("Demonstrating TeaModel and TeaException...")
try:
# Valid request
req1 = MyRequest(my_param='test_value')
req1.validate()
print(f"Request 1 (valid): {req1.my_param}")
# Invalid request (missing my_param)
req2 = MyRequest()
req2.validate()
except TeaException as e:
print(f"Caught TeaException: Code={e.code}, Message={e.message}")
except Exception as e:
print(f"Caught unexpected exception: {e}")
if __name__ == '__main__':
main()