PythonProjectResourceBuilderExtensions.AddPythonProject Method

Definition

Overloads

AddPythonProject(IDistributedApplicationBuilder, String, String, String, String[])

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

AddPythonProject(IDistributedApplicationBuilder, String, String, String, String, String[])

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

AddPythonProject(IDistributedApplicationBuilder, String, String, String, String[])

Source:
PythonProjectResourceBuilderExtensions.cs

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

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

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.

scriptArgs
String[]

The arguments for the script.

Returns

A reference to the IResourceBuilder<T>.

Examples

Add a python project to the application model. In this example the project 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 enviroment 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.AddPythonProject("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 AddPythonProject(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

AddPythonProject(IDistributedApplicationBuilder, String, String, String, String, String[])

Source:
PythonProjectResourceBuilderExtensions.cs

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

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

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 project to the application model. In this example the project 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 enviroment 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.AddPythonProject("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