Clone, run, and explore this sample
Simple Python script demonstrating Aspire's Python integration with automatic virtual environment management.
The entry point that composes every resource and dependency in this sample's distributed application.
import { createBuilder } from "./.aspire/modules/aspire.mjs";
const builder = await createBuilder();
await builder.addPythonApp("script", "./script", "main.py");
await builder.build().run();This sample shows how Aspire 13's Python support automatically manages Python applications, including virtual environment creation and dependency installation.
Quick start
Section titled Quick startPrerequisites
Section titled PrerequisitesCommands
Section titled Commandsaspire run # Run locallyOverview
Section titled OverviewThe application consists of:
- Aspire AppHost - Orchestrates the Python script
- Python Script - Simple console application that processes messages
Key code
Section titled Key codeThe apphost.mts configuration shows the minimal setup for Python scripts:
import { createBuilder } from "./.aspire/modules/aspire.mjs";
const builder = await createBuilder();
await builder.addPythonApp("script", "./script", "main.py");
await builder.build().run();What aspire's python integration does
Section titled What aspire's python integration doesWhen you run this sample, Aspire's Python integration automatically:
Creates a Virtual Environment:
- Aspire detects your Python application and creates a
.venvdirectory - This isolates the Python environment from your system Python installation
- Aspire detects your Python application and creates a
Runs Your Script:
- Aspire executes your Python script using the virtual environment's Python interpreter
- Example:
.venv/bin/python main.py(or.venv/Scripts/python.exeon Windows) - No dependencies needed: This sample has no external dependencies, just pure Python!
Provides Environment Variables:
OTEL_SERVICE_NAME: Service name for observability- Other Aspire-specific variables for integration
Displays Output:
- All console output appears in the Aspire Dashboard
- Logs are aggregated with other services
Note: This sample intentionally has no requirements.txt or pyproject.toml to demonstrate running simple Python scripts with just a virtual environment and no external dependencies.
Key features
Section titled Key features- Minimal Configuration: Just point to your Python script - Aspire handles the rest
- Virtual Environment Management: Automatic
.venvcreation and activation - No Web Server: Demonstrates running Python console applications
- Environment Integration: Aspire provides standard environment variables
- Logging: Script output appears in the Aspire Dashboard console view
How it works
Section titled How it works- Initialization: Aspire creates a virtual environment in
./script/.venv - Execution: The script runs using the virtual environment's Python (no dependencies to install!)
- Lifecycle: The script runs once and completes
- Dashboard Integration: All output is visible in the Aspire Dashboard
VS code integration
Section titled VS code integrationThis sample includes VS Code configuration for Python development:
.vscode/settings.json: Configures the Python interpreter to use the Aspire-created virtual environment- After running
aspire run, open the sample in VS Code for full IntelliSense support - The virtual environment at
script/.venvwill be automatically detected
Running without aspire
Section titled Running without aspireThe Python script is just a normal Python application. You can also run it directly:
cd scriptpython main.pyHowever, Aspire adds:
- Automatic virtual environment management
- Integrated logging and observability
- Environment variable injection
- Unified dashboard view