次の方法で共有


PythonAppResourceBuilderExtensions.WithUv<T> Method

Definition

Adds a UV environment setup task to ensure the virtual environment exists before running the Python application.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithUv<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, bool install = true, string[]? args = default) where T : Aspire.Hosting.Python.PythonAppResource;
static member WithUv : 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 WithUv(Of T As PythonAppResource) (builder As IResourceBuilder(Of T), Optional install As Boolean = true, Optional args 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 runs uv sync before the application starts. When false, only sets the package manager annotation without creating an installer resource.

args
String[]

Optional custom arguments to pass to the uv command. If not provided, defaults to ["sync"].

Returns

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

Exceptions

Thrown when builder is null.

Examples

Add a Python app with automatic UV environment setup:

var builder = DistributedApplication.CreateBuilder(args);

var python = builder.AddPythonScript("api", "../python-api", "main.py")
    .WithUv()  // Automatically runs 'uv sync' before starting the app
    .WithHttpEndpoint(port: 5000);

builder.Build().Run();

Add a Python app with custom UV arguments:

var builder = DistributedApplication.CreateBuilder(args);

var python = builder.AddPythonScript("api", "../python-api", "main.py")
    .WithUv(args: ["sync", "--python", "3.12", "--no-dev"])  // Custom uv sync args
    .WithHttpEndpoint(port: 5000);

builder.Build().Run();

Remarks

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

UV (https://github.com/astral-sh/uv) is a modern Python package manager written in Rust that can manage virtual environments and dependencies with significantly faster performance than traditional tools. The uv sync command ensures that the virtual environment exists, Python is installed if needed, and all dependencies specified in pyproject.toml are installed and synchronized.

Calling this method will replace any previously configured package manager (such as pip).

Applies to