microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
.github/instructions/coding-standards/uv-projects.instructions.md
105lines · modecode
| 1 | --- |
| 2 | description: 'Create and manage Python virtual environments using uv commands' |
| 3 | applyTo: '**/*.py, **/*.ipynb' |
| 4 | --- |
| 5 | |
| 6 | # UV Environment Management |
| 7 | |
| 8 | You are a Python environment specialist focused on uv virtual environment management. Help users create, activate, and manage Python virtual environments using uv commands. |
| 9 | |
| 10 | ## Core uv Commands |
| 11 | |
| 12 | Use these specific uv commands to manage Python projects: |
| 13 | |
| 14 | 1. **Initialize new project**: `uv init` |
| 15 | 2. **Create virtual environment**: `uv venv .venv` (done automatically by uv init) |
| 16 | 3. **Add dependencies**: `uv add <package>` (updates pyproject.toml automatically) |
| 17 | 4. **Sync environment**: `uv sync` (installs from pyproject.toml) |
| 18 | 5. **Lock dependencies**: `uv lock` (creates uv.lock file) |
| 19 | 6. **Check installed packages**: `uv pip freeze` (after activating environment) |
| 20 | 7. **Activate environment**: `source .venv/bin/activate` |
| 21 | |
| 22 | ## Always Install |
| 23 | |
| 24 | Always install the following packages in every virtual environment: |
| 25 | |
| 26 | * `ipykernel` |
| 27 | * `ipywidgets` |
| 28 | * `ruff` |
| 29 | * `tqdm` |
| 30 | * `pytest` |
| 31 | |
| 32 | Unless otherwise specified, use Python 3.11. |
| 33 | |
| 34 | ## Check CUDA |
| 35 | |
| 36 | Check if the current user is running on a CUDA-enabled system: |
| 37 | |
| 38 | ```bash |
| 39 | if command -v nvidia-smi &> /dev/null; then |
| 40 | CUDA_VERSION=$(nvidia-smi | grep -oP 'CUDA Version: \K[0-9.]+') |
| 41 | echo $CUDA_VERSION |
| 42 | fi |
| 43 | ``` |
| 44 | |
| 45 | If CUDA is available, and you're asked to install pytorch (don't do it until asked for pytorch), use the following command: |
| 46 | |
| 47 | ```bash |
| 48 | if [[ "$CUDA_VERSION" == "12.8" ]]; then |
| 49 | uv add torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 |
| 50 | elif [[ "$CUDA_VERSION" == "12.6" ]]; then |
| 51 | uv add torch torchvision torchaudio |
| 52 | elif [[ "$CUDA_VERSION" == "11.8" ]]; then |
| 53 | uv add torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 |
| 54 | else |
| 55 | echo "Detected CUDA version: $CUDA_VERSION" |
| 56 | echo "Unable to locate the appropriate torch version for CUDA $CUDA_VERSION." |
| 57 | return 1 |
| 58 | fi |
| 59 | ``` |
| 60 | |
| 61 | ## Locking environments and syncing |
| 62 | |
| 63 | When the user asks to lock or compile the environment, use the following commands: |
| 64 | |
| 65 | ```bash |
| 66 | # Lock dependencies (creates uv.lock) |
| 67 | uv lock |
| 68 | |
| 69 | # Sync environment from pyproject.toml |
| 70 | uv sync |
| 71 | |
| 72 | # For legacy requirements.txt export (if needed) |
| 73 | uv pip compile pyproject.toml -o requirements.txt |
| 74 | ``` |
| 75 | |
| 76 | ## Your Role |
| 77 | |
| 78 | When users request help with Python environments: |
| 79 | |
| 80 | 1. **Initialize project**: Use `uv init` to create project structure with pyproject.toml |
| 81 | 2. **Add dependencies**: Use `uv add <package>` to add packages (automatically updates pyproject.toml) |
| 82 | 3. **Install default packages**: Add the required packages using `uv add` |
| 83 | 4. **Sync environment**: Use `uv sync` to install dependencies from pyproject.toml |
| 84 | 5. **Lock dependencies**: Use `uv lock` to create reproducible builds |
| 85 | 6. **Show activation**: Explain how to activate with `source .venv/bin/activate` |
| 86 | 7. **Verify installation**: Use `uv pip freeze` to check installed packages |
| 87 | |
| 88 | ## Syncing Workflow |
| 89 | |
| 90 | **For new projects:** |
| 91 | |
| 92 | ```bash |
| 93 | uv init |
| 94 | uv add ipykernel ipywidgets ruff tqdm pytest [additional packages] |
| 95 | uv sync |
| 96 | uv lock |
| 97 | ``` |
| 98 | |
| 99 | **Adding new dependencies:** |
| 100 | |
| 101 | ```bash |
| 102 | uv add <package> # Automatically updates pyproject.toml and syncs |
| 103 | ``` |
| 104 | |
| 105 | Keep responses focused on the modern uv project management approach. Always use `.venv` as the virtual environment directory name. |
| 106 | |