Install the Vertex AI SDK for Python

Use the Vertex AI SDK for Python to automate your machine learning (ML) workflows. This topic shows you how to install the Vertex AI SDK for Python. For more information about the Vertex AI SDK, see the following resources:

Installation of the Vertex AI SDK for Python includes the following steps:

  1. Create an isolated Python environment
  2. Install the Vertex AI SDK package
  3. Initialize the Vertex AI SDK

Create an isolated Python environment

A Python best practice is to install the Vertex AI SDK in an isolated Python environment for each project. This helps prevent dependency, version, and permissions conflicts. You can create an isolated environment for using the command line in a shell or for using a notebook.

To create an isolated environment when you use the command line, activate a venv environment. After the venv environment is activated, you're ready to install the Vertex AI SDK and run your Python scripts. For more information, see Use venv to isolate dependencies and Set up a Python development environment.

To use a notebook for your isolated environment, create a notebook instance. After you create your notebook instance, use it to install the Vertex AI SDK and run your Python scripts. For more information, see Create a user-managed notebooks instance.

Install or update the Vertex AI SDK package

To install or update the Vertex AI SDK, run the following command in your virtual environment:

pip install --upgrade google-cloud-aiplatform

Initialize the Vertex AI SDK

After you install the Vertex AI SDK for Python, you must initialize the SDK with your Vertex AI and Google Cloud details. For example, when you initialize the SDK, you specify information such as your project name, region, and your staging Cloud Storage bucket. The following method is an example of a method that initializes the Vertex AI SDK.

def init_sample(
    project: Optional[str] = None,
    location: Optional[str] = None,
    experiment: Optional[str] = None,
    staging_bucket: Optional[str] = None,
    credentials: Optional[auth_credentials.Credentials] = None,
    encryption_spec_key_name: Optional[str] = None,
    service_account: Optional[str] = None,
):

    from google.cloud import aiplatform

    aiplatform.init(
        project=project,
        location=location,
        experiment=experiment,
        staging_bucket=staging_bucket,
        credentials=credentials,
        encryption_spec_key_name=encryption_spec_key_name,
        service_account=service_account,
    )

What's next