Repository navigation
Add WithUvEnvironment extension method to Aspire.Hosting.Python - #11937
Conversation
Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
WithPythonUvEnvironment extension method to Aspire.Hosting.Python
|
Copilot implement the PR feedback. |
…ernal, remove IResourceWithParent Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
WithPythonUvEnvironment extension method to Aspire.Hosting.PythonWithUvEnvironment extension method to Aspire.Hosting.Python
|
Update the python playgroud sample to use the UseUvEnvironment method and remove https://github.com/dotnet/aspire/blob/main/playground/AspireWithPython/AspireWithPython.AppHost/AspireWithPython.AppHost.csproj |
|
Copilot implement the review feedback. |
Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
…Environment Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
Updated |
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 11937Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 11937" |
There was a problem hiding this comment.
Pull Request Overview
This PR adds UV (modern Python package manager) support to the Aspire.Hosting.Python package by implementing a WithUvEnvironment extension method that automatically sets up UV-managed virtual environments before Python application startup.
- Introduces
PythonUvEnvironmentResourceas an internal resource class that executesuv synccommands - Adds
WithUvEnvironment<T>()extension method for configuring UV environment setup with proper dependency management - Updates the AspireWithPython playground sample to demonstrate the new functionality and removes manual MSBuild virtual environment setup
Reviewed Changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Aspire.Hosting.Python/PythonUvEnvironmentResource.cs | New internal resource class that extends ExecutableResource to run UV sync commands |
| src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs | Implements the WithUvEnvironment extension method with proper resource relationships |
| src/Aspire.Hosting.Python/Aspire.Hosting.Python.csproj | Adds InternalsVisibleTo attribute for test access |
| tests/Aspire.Hosting.Python.Tests/AddPythonAppTests.cs | Comprehensive test coverage for the new UV environment functionality |
| playground/AspireWithPython/AspireWithPython.AppHost/AppHost.cs | Updates playground to use WithUvEnvironment method |
| playground/AspireWithPython/AspireWithPython.AppHost/AspireWithPython.AppHost.csproj | Removes manual MSBuild Python environment setup targets |
| playground/AspireWithPython/InstrumentedPythonProject/pyproject.toml | Adds UV-compatible project configuration file |
| playground/AspireWithPython/InstrumentedPythonProject/requirements.txt | Removes requirements.txt in favor of pyproject.toml dependencies |
| playground/AspireWithPython/InstrumentedPythonProject/.python-version | Specifies Python 3.13 version for UV |
| @@ -0,0 +1 @@ | |||
| 3.13 | |||
There was a problem hiding this comment.
For my learning - why does this need to go here and in the pyproject.toml file?
There was a problem hiding this comment.
This is like global json, uv will install this python version if this file is here
https://docs.astral.sh/uv/concepts/python-versions/#python-version-files
though we don’t bootstrap python with uv (yet)
Implements the
WithUvEnvironmentextension method for Python applications in .NET Aspire, enabling automatic UV-managed virtual environment setup before application startup.Overview
This PR adds support for UV (https://github.com/astral-sh/uv), a modern Python package manager, to ensure virtual environments and dependencies are properly configured before Python applications start.
Changes
New Resource Type:
PythonUvEnvironmentResourceAn internal sealed resource class that represents a UV environment setup task. It extends
ExecutableResourceto run theuv synccommand. The resource establishes a parent-child relationship with the Python application through annotations and is excluded from manifest generation.New Extension Method:
WithUvEnvironment<T>()The extension method can be chained on any Python app resource to automatically set up a UV-managed environment:
Behavior
{pythonAppName}-uv-environmentuv syncin the Python app's working directoryWaitForCompletiondependency ensuring the Python app waits for UV sync to complete successfully before startingPlayground Sample Updated
Updated the
AspireWithPythonplayground sample to demonstrate the newWithUvEnvironment()method. The playground's AppHost project has been simplified by removing manual MSBuild targets for Python virtual environment setup, as this is now handled automatically byWithUvEnvironment().Testing
Added comprehensive test coverage including:
uv sync)All 39 tests in the Python hosting test suite pass successfully.
Fixes
Fixes #11934
Original prompt
Fixes #11934
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.