次の方法で共有


PythonAppResourceBuilderExtensions.WithPip<T> Method

Definition

Configures the Python resource to use pip as the package manager and optionally installs packages before the application starts.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithPip<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, bool install = true, string[]? installArgs = default) where T : Aspire.Hosting.Python.PythonAppResource;
static member WithPip : Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.Python.PythonAppResource)> * bool * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.Python.PythonAppResource)> (requires 'T :> Aspire.Hosting.Python.PythonAppResource)
<Extension()>
Public Function WithPip(Of T As PythonAppResource) (builder As IResourceBuilder(Of T), Optional install As Boolean = true, Optional installArgs As String() = Nothing) As IResourceBuilder(Of T)

Type Parameters

T

The type of the Python application resource, must derive from PythonAppResource.

Parameters

builder
IResourceBuilder<T>

The resource builder.

install
Boolean

When true (default), automatically installs packages before the application starts. When false, only sets the package manager annotation without creating an installer resource.

installArgs
String[]

The command-line arguments passed to pip install command.

Returns

A reference to the IResourceBuilder<T> for method chaining.

Exceptions

Thrown when builder is null.

Examples

Add a Python app with automatic pip package installation:

var builder = DistributedApplication.CreateBuilder(args);

var python = builder.AddPythonScript("api", "../python-api", "main.py")
    .WithPip()  // Automatically installs from pyproject.toml or requirements.txt
    .WithHttpEndpoint(port: 5000);

builder.Build().Run();

Remarks

This method creates a child resource that runs pip install in the working directory of the Python application. The Python application will wait for this resource to complete successfully before starting.

Pip will automatically detect and use either pyproject.toml or requirements.txt based on which file exists in the application directory. If pyproject.toml exists, pip will use it. Otherwise, if requirements.txt exists, pip will use that. Calling this method will replace any previously configured package manager (such as uv).

Applies to