Share via


PythonAppResourceBuilderExtensions.AddPythonModule Method

Definition

Adds a Python module to the application model.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonModule(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string moduleName);
static member AddPythonModule : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
<Extension()>
Public Function AddPythonModule (builder As IDistributedApplicationBuilder, name As String, appDirectory As String, moduleName As String) As IResourceBuilder(Of PythonAppResource)

Parameters

name
String

The name of the resource.

appDirectory
String

The path to the directory containing the python application.

moduleName
String

The name of the Python module to run (e.g., "flask", "uvicorn").

Returns

A reference to the IResourceBuilder<T>.

Examples

Add a Flask module to the application model:

var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonModule("flask-dev", "../flaskapp", "flask")
       .WithArgs("run", "--debug", "--host=0.0.0.0");

builder.Build().Run();

Remarks

This method runs a Python module using python -m <module>. By default, the virtual environment folder is expected to be named .venv and located in the app directory. Use WithVirtualEnvironment<T>(IResourceBuilder<T>, String, Boolean) to specify a different virtual environment path. Use WithArgs to pass arguments to the module.

Python modules automatically have debugging support enabled.

Applies to