共用方式為


PythonAppResourceBuilderExtensions.AddPythonApp Method

Definition

Overloads

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String[])

Adds a python application with a virtual environment to the application model.

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[])

Adds a python application with a virtual environment to the application model.

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String[])

Source:
PythonAppResourceBuilderExtensions.cs

Adds a python application with a virtual environment to the application model.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp (this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, params string[] scriptArgs);
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, projectDirectory As String, scriptPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)

Parameters

name
String

The name of the resource.

projectDirectory
String

The path to the directory containing the python app files.

scriptPath
String

The path to the script relative to the project directory to run.

scriptArgs
String[]

The arguments for the script.

Returns

A reference to the IResourceBuilder<T>.

Examples

Add a python app or executable to the application model. In this example the python code entry point is located in the PythonProject directory if this path is relative then it is assumed to be relative to the AppHost directory, and the virtual environment path if relative is relative to the project directory. In the example below, if the app host directory is $HOME/repos/MyApp/src/MyApp.AppHost then the ProjectPath would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject and the virtual environment path (defaulted) would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject/.venv.

var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonApp("python-project", "PythonProject", "main.py");

builder.Build().Run(); 

Remarks

The virtual environment must be initialized before running the project. By default the virtual environment folder is expected to be named .venv and be located in the project directory. If the virtual environment is located in a different directory this default can be specified by using the AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[]) overload of this method.

The virtual environment is setup individually for each project to allow each project to use a different version of Python and dependencies. To setup a virtual environment use the python -m venv .venv command in the project directory. This will create a virtual environment in the .venv directory.

To restore dependencies in the virtual environment first activate the environment by executing the activation script and then use the pip install -r requirements.txt command to restore dependencies.

To receive traces, logs, and metrics from the python project in the dashboard, the project must be instrumented with OpenTelemetry. You can instrument your project by adding the opentelemetry-distro, and opentelemetry-exporter-otlp to your Python project.

Applies to

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[])

Source:
PythonAppResourceBuilderExtensions.cs

Adds a python application with a virtual environment to the application model.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp (this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, string virtualEnvironmentPath, params string[] scriptArgs);
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, projectDirectory As String, scriptPath As String, virtualEnvironmentPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)

Parameters

name
String

The name of the resource.

projectDirectory
String

The path to the directory containing the python project files.

scriptPath
String

The path to the script relative to the project directory to run.

virtualEnvironmentPath
String

Path to the virtual environment.

scriptArgs
String[]

The arguments for the script.

Returns

A reference to the IResourceBuilder<T>.

Examples

Add a python app or executable to the application model. In this example the python code is located in the PythonProject directory if this path is relative then it is assumed to be relative to the AppHost directory, and the virtual environment path if relative is relative to the project directory. In the example below, if the app host directory is $HOME/repos/MyApp/src/MyApp.AppHost then the ProjectPath would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject and the virtual environment path (defaulted) would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject/.venv.

var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonApp("python-project", "PythonProject", "main.py");

builder.Build().Run(); 

Remarks

The virtual environment is setup individually for each project to allow each project to use a different version of Python and dependencies. To setup a virtual environment use the python -m venv .venv command in the project directory. This will create a virtual environment in the .venv directory (where .venv is the name of your virtual environment directory).

To restore dependencies in the virtual environment first activate the environment by executing the activation script and then use the pip install -r requirements.txt command to restore dependencies.

To receive traces, logs, and metrics from the python project in the dashboard, the project must be instrumented with OpenTelemetry. You can instrument your project by adding the opentelemetry-distro, and opentelemetry-exporter-otlp to your Python project.

Applies to