如果您是经验丰富的机器学习 (ML) 及人工智能 (AI) 工程师或是希望以编程方式自动执行工作流的数据科学家,则推荐您使用 Vertex AI SDK for Python。Vertex AI SDK for Python 与 Vertex AI Python 客户端库类似,只不过 Vertex AI SDK 内容更具概括性,没有那么细化。如需了解详情,请参阅了解 SDK 和客户端库的区别。
使用 Vertex AI SDK for Python 编写代码
如需使用 Vertex AI SDK for Python,请执行以下操作:
在虚拟环境中运行以下命令,安装 google-cloud-aiplatform 软件包(其中包含 Vertex AI SDK for Python 和 Vertex AI Python 客户端库):
pipinstall--upgradegoogle-cloud-aiplatform
使用以下代码导入 google.cloud.aiplatform 命名空间:
fromgoogle.cloudimportaiplatform
If you're using a local shell, then create local authentication credentials for your user
account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
安装 Vertex AI SDK for Python 时,会同时安装 Vertex AI Python 客户端库。Vertex AI SDK 和 Vertex AI Python 客户端库具有粒度级别不同的类似功能。Vertex AI SDK 以比客户端库更高的抽象层运行,适合大多数常见的数据科学工作流。如果您需要较低级别的功能,请使用 Vertex AI Python 客户端库。
Vertex AI SDK 适用于 Python,Vertex AI 客户端库适用于 Python、Java 和 Node.js。如需了解如何安装 Java 或 Node.js 客户端库,请参阅安装 Vertex AI 客户端库。如果您的首选编程语言不支持客户端库,则可以使用 Vertex AI REST API。如需了解详情,请参阅 Vertex AI REST 参考文档。
结合使用 Vertex AI Python 客户端库和 SDK
如果您在使用 Vertex AI SDK for Python 时发现自己需要更高的灵活性或掌控权,抑或需要使用未包含在 Vertex AI SDK 中的方法,您便可以在同一工作流中使用 Vertex AI Python 客户端库来解决这些问题。Vertex AI Python 客户端库使用不同的命名空间来访问 Vertex AI API。您可以在同一 Python 脚本中使用客户端库命名空间和 Vertex AI SDK for Python 命名空间,只需在 Python 脚本中为这两者各添加一行 import 内容即可。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-28。"],[],[],null,["# Introduction to the Vertex AI SDK for Python\n\nThe Vertex AI SDK for Python helps you automate data ingestion, train models, and\nget predictions on Vertex AI. The Vertex AI SDK uses Python code to\naccess the Vertex AI API so that you can programmatically accomplish most of what\nyou can do in the Google Cloud console.\n\nTo learn how to install or update the Vertex AI SDK for Python, see [Install the\nVertex AI SDK for Python](/vertex-ai/docs/start/install-sdk). For more information, see\nthe [Vertex AI SDK for Python API reference documentation](/python/docs/reference/aiplatform/latest).\n\nWhy use the Vertex AI SDK\n-------------------------\n\nThe Vertex AI SDK for Python is recommended if you're an experienced machine\nlearning (ML) and artificial intelligence (AI) engineer or a data scientist who\nwants to programmatically automate your workflow. The Vertex AI SDK for Python is\nsimilar to the Vertex AI Python client library, except the\nVertex AI SDK is higher-level and less granular. For more information, see\n[Understand the SDK and client library differences](#sdk-vs-client-library).\n\n\u003cbr /\u003e\n\nWrite code with the Vertex AI SDK for Python\n--------------------------------------------\n\nTo use the Vertex AI SDK for Python:\n\n1. Install the `google-cloud-aiplatform` package, which includes both the Vertex AI SDK for Python and the Vertex AI Python client library, by running the following command in your virtual environment:\n\n pip install --upgrade google-cloud-aiplatform\n\n2. Use the following code to import the `google.cloud.aiplatform` namespace:\n\n from google.cloud import aiplatform\n\n | **Preview:** To use features for the Vertex AI SDK for Python that are still in [preview](/products#product-launch-stages), import `vertexai.preview`: \n |\n | ```python\n | import vertexai.preview\n | ```\n\n \u003cbr /\u003e\n\n3.\n\n If you're using a local shell, then create local authentication credentials for your user\n account:\n\n ```bash\n gcloud auth application-default login\n ```\n\n You don't need to do this if you're using Cloud Shell.\n\n\n If an authentication error is returned, and you are using an external identity provider\n (IdP), confirm that you have\n [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n### Learn about the Vertex AI SDK for Python\n\nSee the following documentation:\n\n- [Vertex AI SDK class overview](/vertex-ai/docs/python-sdk/python-sdk-class-overview): introduces the key classes and functionality in the Vertex AI SDK.\n\n- [Python reference for Vertex AI](/python/docs/reference/aiplatform/latest): contains reference documentation for all of the namespaces, classes, methods, and properties in the `google-cloud-aiplatform` package, which includes the Vertex AI SDK, the Vertex AI SDK preview, and the Vertex AI Client libraries.\n\n### Try code samples and tutorials\n\nNotebook tutorials show how to use the Vertex AI SDK for Python as part of a\nlarger workflow. For more information, see\n[Vertex AI notebook tutorials](/vertex-ai/docs/tutorials/jupyter-notebooks).\n\nCode samples in the Vertex AI SDK for Python GitHub repository show you how to\ncomplete individual tasks. For more information, see the\n[Vertex AI SDK for Python GitHub repository](https://github.com/googleapis/python-aiplatform/). \n| To see an example of using the Vertex AI SDK as part of a more comprehensive workflow,\n| run the \"Custom training and online prediction\" notebook in one of the following\n| environments:\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fsdk-custom-image-classification-online.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fsdk-custom-image-classification-online.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb)\n\nUnderstand the Vertex AI SDK and client library differences\n-----------------------------------------------------------\n\nWhen you install the Vertex AI SDK for Python, the Vertex AI Python client\nlibrary is also installed. The Vertex AI SDK and the\nVertex AI Python client library provide similar functionality with\ndifferent levels of granularity. The Vertex AI SDK operates at a\nhigher level of abstraction than the client library and is suitable for most\ncommon data science workflows. If you need lower-level functionality, then use\nthe Vertex AI Python client library.\n\nThe Vertex AI SDK is available for Python and a Vertex AI client\nlibrary is available for Python, Java, and Node.js. To learn how to install the\nJava or Node.js client library, see\n[Install the Vertex AI client libraries](/vertex-ai/docs/start/client-libraries).\nIf a client library isn't available in your preferred programming language, you\ncan use the Vertex AI REST API. For more information, see the\n[Vertex AI REST reference](/vertex-ai/docs/reference/rest).\n\n### Use Vertex AI Python client library and SDK together\n\nIf you use the Vertex AI SDK for Python and discover you need greater flexibility\nor control, or if you need a method not included in the Vertex AI SDK, you\ncan use the Vertex AI Python client library in the same workflow. The\nVertex AI Python client library uses a different namespace to access the\nVertex AI API. The client library and the Vertex AI SDK for Python\nnamespaces can be used in the same Python script by adding an `import` line for\neach in your Python script.\n\n### Import the Vertex AI Python client library namespace\n\nThe Vertex AI Python client library namespace is\n`google.cloud.aiplatform.gapic`. This namespace maps to the\n`google.cloud.aiplatform_v1` namespace. These two namespaces can be used\ninterchangeably. To import the Python client library, include one of the\nfollowing in your Python script: \n\n```\nfrom google.cloud import aiplatform_v1\n``` \n\n```\nfrom google.cloud.aiplatform import gapic\n```\n\nWhat's next\n-----------\n\n- Learn how to [choose a training method](/vertex-ai/docs/start/training-methods)."]]