Simple Python script demonstrating Aspire's Python integration with automatic virtual environment management.
This sample shows how Aspire 13's Python support automatically manages Python applications, including virtual environment creation and dependency installation.
aspire run # Run locallyThe application consists of:
- Aspire AppHost - Orchestrates the Python script
- Python Script - Simple console application that processes messages
The apphost.ts configuration shows the minimal setup for Python scripts:
import { createBuilder } from "./.modules/aspire.js";
const builder = await createBuilder();
await builder.addPythonApp("script", "./script", "main.py");
await builder.build().run();When 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.
- 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
- 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
This 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
The Python script is just a normal Python application. You can also run it directly:
cd script
python main.pyHowever, Aspire adds:
- Automatic virtual environment management
- Integrated logging and observability
- Environment variable injection
- Unified dashboard view