PythonAppResourceBuilderExtensions.AddPythonApp Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| AddPythonApp(IDistributedApplicationBuilder, String, String, String) |
Adds a Python application to the application model. |
| AddPythonApp(IDistributedApplicationBuilder, String, String, String, String[]) |
Obsolete.
Adds a python application with a virtual environment to the application model. |
| AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[]) |
Obsolete.
Adds a python application with a virtual environment to the application model. |
AddPythonApp(IDistributedApplicationBuilder, String, String, String)
Adds a Python application to the application model.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath);
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, appDirectory As String, scriptPath As String) As IResourceBuilder(Of PythonAppResource)
Parameters
- builder
- IDistributedApplicationBuilder
The IDistributedApplicationBuilder to add the resource to.
- name
- String
The name of the resource.
- appDirectory
- String
The path to the directory containing the python application.
- scriptPath
- String
The path to the script relative to the app directory to run.
Returns
A reference to the IResourceBuilder<T>.
Examples
Add a FastAPI Python application to the application model:
var builder = DistributedApplication.CreateBuilder(args);
builder.AddPythonApp("fastapi-app", "../api", "main.py")
.WithArgs("arg1", "arg2");
builder.Build().Run();
Remarks
This method executes a Python script directly using python script.py. By default, the virtual environment is resolved using the following priority:
WithArgs to pass arguments to the script.Python applications automatically have debugging support enabled.
Applies to
AddPythonApp(IDistributedApplicationBuilder, String, String, String, String[])
Caution
Use AddPythonScript, AddPythonModule, or AddPythonExecutable and chain with .WithArgs(...) instead.
Adds a python application with a virtual environment to the application model.
[System.Obsolete("Use AddPythonScript, AddPythonModule, or AddPythonExecutable and chain with .WithArgs(...) instead.")]
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath, params string[] scriptArgs);
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath, params string[] scriptArgs);
[<System.Obsolete("Use AddPythonScript, AddPythonModule, or AddPythonExecutable and chain with .WithArgs(...) instead.")>]
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
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, appDirectory As String, scriptPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)
Parameters
- builder
- IDistributedApplicationBuilder
The IDistributedApplicationBuilder to add the resource to.
- name
- String
The name of the resource.
- appDirectory
- String
The path to the directory containing the python app files.
- scriptPath
- String
The path to the script relative to the app directory to run.
- scriptArgs
- String[]
The arguments for the script.
Returns
A reference to the IResourceBuilder<T>.
- Attributes
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
This overload is obsolete. Use one of the more specific methods instead:
- AddPythonApp(IDistributedApplicationBuilder, String, String, String) - To run a Python script file
- AddPythonModule(IDistributedApplicationBuilder, String, String, String) - To run a Python module via
python -m - AddPythonExecutable(IDistributedApplicationBuilder, String, String, String) - To run an executable from the virtual environment
Chain with WithArgs to pass arguments:
builder.AddPythonScript("name", "dir", "script.py")
.WithArgs("arg1", "arg2");
Applies to
AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[])
Caution
Use AddPythonScript, AddPythonModule, or AddPythonExecutable and chain with .WithVirtualEnvironment(...).WithArgs(...) instead.
Adds a python application with a virtual environment to the application model.
[System.Obsolete("Use AddPythonScript, AddPythonModule, or AddPythonExecutable and chain with .WithVirtualEnvironment(...).WithArgs(...) instead.")]
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath, string virtualEnvironmentPath, params string[] scriptArgs);
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath, string virtualEnvironmentPath, params string[] scriptArgs);
[<System.Obsolete("Use AddPythonScript, AddPythonModule, or AddPythonExecutable and chain with .WithVirtualEnvironment(...).WithArgs(...) instead.")>]
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
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, appDirectory As String, scriptPath As String, virtualEnvironmentPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)
Parameters
- builder
- IDistributedApplicationBuilder
The IDistributedApplicationBuilder to add the resource to.
- name
- String
The name of the resource.
- appDirectory
- String
The path to the directory containing the python app files.
- scriptPath
- String
The path to the script to run, relative to the app directory.
- virtualEnvironmentPath
- String
Path to the virtual environment.
- scriptArgs
- String[]
The arguments for the script.
Returns
A reference to the IResourceBuilder<T>.
- Attributes
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
This overload is obsolete. Use one of the more specific methods instead:
- AddPythonApp(IDistributedApplicationBuilder, String, String, String) - To run a Python script file
- AddPythonModule(IDistributedApplicationBuilder, String, String, String) - To run a Python module via
python -m - AddPythonExecutable(IDistributedApplicationBuilder, String, String, String) - To run an executable from the virtual environment
Chain with WithVirtualEnvironment<T>(IResourceBuilder<T>, String, Boolean) and WithArgs:
builder.AddPythonScript("name", "dir", "script.py")
.WithVirtualEnvironment("myenv")
.WithArgs("arg1", "arg2");