Skip to content

AddPythonApp issues #6237

Closed
7 of 7 issues completed
Closed

Description

The current AddPythonApp methods have a few usability issues:

  • When it was renamed from AddPythonProject, the projectDirectory parameter wasn't renamed to appDirectory

  • The two overloads both have only string parameters and finish with a params string[] parameter, which means the compiler can't differentiate between them unless the params arg is passed as a string[]. Trying to call the overload with the shorter parameter count with at least one argument for the params parameter, results in the compiler binding to the overload with the longer parameter count:

    // What I want to type to call './PythonApp/main.py arg1 arg2'
    builder.AddPythonApp("python-app", "../PythonApp", "main.py", "arg1", "arg2");
    
    // This actually binds to the overload that accepts the location of the virtual environment directory, so this is actually what happens.
    // This results in a runtime exception as the python resource verifies the specified virtual environment path exists and throws if not.
    builder.AddPythonApp(name: "python-app", projectDirectory: "./PythonApp", scriptPath: "main.py", virtualEnvironmentPath: "arg1", "arg2");
    
    // This is the workaround
    builder.AddPythonApp("python-app", "../PythonApp", "main.py", ["arg1", "arg2"]);

    Image

  • There's no first-class overload or different method to support running python apps where a module is the entrypoint and figuring out how to invoke the overloads that are there such that a module is used as the entrypoint is confusing due to the parameter counts and names. When combined with the previous point, this is what is required to run a Python app using a module as the entrypoint:

    // You need to pass "-m" as the value for the 'scriptPath' parameter:
    builder.AddPythonApp("python-app-module", "../PythonApp", scriptPath: "-m", ["module-name", "moduleArg1"]);
  • The integration requires the use of Python virtual environments, but not all Python apps use them. Some apps use no virtual environments, others use virtual environments managed by a different toolchain like Miniconda. Furthermore, the virtual environment is not created or activated automatically by the integration. As the recommendation is to not include the virtual environment directory in source control, the user must manually create and activate the virtual environment, and install dependencies into it, before launching the app host project, after cloning or cleaning their local clone. We should consider:

    • Supporting Python apps that don't use virtual environments or use virtual environments and/or package managers other than Pip
    • Doing more to automate bootstrapping virtual environments for apps that use them
  • Similar to the point above about running a Python app where a module is the entry point, some Python apps use a completely different executable as the entry point, i.e., something other than python. Indeed on Ubuntu and other distros the default python command is python3, not python. Frameworks like Flask have their own command, i.e., flask.

  • PythonAppResource is in the namespace Aspire.Hosting.Python, whereas NodeAppResource is directly in Aspire.Hosting. This is inconsistent, we should put PythonAppResource in Aspire.Hosting.

Activity

  1. added this to the 9.0 milestone on Oct 11, 2024
  2. joperezr commented on Oct 14, 2024

    @joperezr
    Member

    Discussed this offline. We should try to minimize the changes that go into 9.0 at this point, so if there are issues with the new API, we should consider marking it as experimental so we get more bake time.

  3. raffaeler commented on Oct 17, 2024

    @raffaeler

    Please consider also the case where the python application fails to run and the console output is completely empty.
    There is also no output in the output AppHost output window.
    Anyway, the same command line used by Aspire, can run successfully when running it from the command prompt.
    I would expect catching the stdout+stderr to the Console window as well.

  4. DamianEdwards commented on Oct 17, 2024

    @DamianEdwards
    MemberAuthor

    Please consider also the case where the python application fails to run and the console output is completely empty.
    There is also no output in the output AppHost output window.

    Hmm I didn't see that particular issue, but note I was using Aspire 9 bits. I definitely saw error logs from the failing Python app in the console logs of the Python resource.

  5. raffaeler commented on Oct 17, 2024

    @raffaeler

    It looks like that when the Python process fails prematurely, the Console does not show anything.
    I tried to put a print at the very beginning but nothing.
    I only see a mysterious exit with error code 1

  6. DamianEdwards commented on Oct 17, 2024

    @DamianEdwards
    MemberAuthor

    How prematurely? Can you share a minimal repro, preferably as a GitHub repo?

  7. raffaeler commented on Oct 17, 2024

    @raffaeler

    The Python code is ok as I can launch it on the cmd.
    It also works correctly using .AddDockerfile.

    I tried reducing the code to the sample Python code shown in AddPythonProject docs which uses flask to listen on 8111.
    While I may have something wrong with the solution, the real issue is the inability to diagnose a failure when launching it.
    I can only see a socket exception in the AppHost output window which does not provide any clue.

    I will try tomorrow (now it's night here) to create a new solution in a public repo.

  8. raffaeler commented on Oct 17, 2024

    @raffaeler

    How prematurely

    It immediately exit at launch time. I suspect it is so fast that the Aspire console is unable to capture the Python exit message (if any).
    The other suspect is that it fails running the process completely but there is no message suggesting it

  9. raffaeler commented on Oct 23, 2024

    @raffaeler

    Damian Edwards (@DamianEdwards) sorry for the delay but I wanted to test the scenario on Aspire9 first:
    https://github.com/raffaeler/aspire9tests

    The issue is still there on the newest bits. It's worth noting:

    1. This project comes from the version 8 templates. I just renamed AddPythonProject to AddPythonApp
    2. On version 9 I see the .AddDockerfile successfully creates the container (inside it runs ok) but it looks inaccessible from the host machine. On Aspire 8 the .AddDockerfile works correctly
    3. I am always running the http profile, but be aware that I created and installed my own localhost certificate chain (not needed in this repro, but it is in the real project). May this be the culprit even if I am on the http profile?

    What is disturbing is the difficulty in diagnosing what's going wrong.
    What's the best way to diagnose the issue here?

    Thanks

  10. 21 remaining items

  11. karolz-ms commented on Dec 19, 2024

    @karolz-ms
    Contributor

    Sorry for taking so long to look into this. My teammate (Devin Breshears (@dbreshears)) was able to get the example running after a few tries, but we noticed something that may explain why you have seen the startup fail with no additional information.

    AddPythonApp() will try to run the app through OpenTelemetry auto-instrumentation wrapper (if it is present). We had a situation where the Python virtual environment was incorrectly configured and the path to python.exe was incorrect. In this case the OTEL wrapper would start, but then almost immediately fail with no output to standard error or standard output. Not a single byte; just exit with code 1. So that is what you would see in the Aspire dashboard, the app exiting with code 1, but no logs. Reflects the truth about what happened, but is not very helpful.

    How we figured it out was by looking at the application command line and parameters (which are visible in the Aspire dashboard) and trying the same thing manually from command line.

  12. MedAnd commented on Aug 6, 2025

    @MedAnd

    Hi, any update on this issue? I am looking to run an Azure Function written in Python and cannot get it to start with "func start" and also does not have the correct virtual environment context.

    For example it starts with the Global Python context, does not find my OpenTelemetry dependencies which are part of the activated virtual environment context etc.

  13. davidfowl commented on Oct 15, 2025

    @davidfowl
    Collaborator

    Few updates form this original issue:

    There's be more in the future.

  14. locked and limited conversation to collaborators on Nov 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-integrationsIssues pertaining to Aspire Integrations packagespython

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions