# Import Verification for Torch, Torchvision, Torchaudio Simultaneously with uv: A Complete Guide

> Easily verify simultaneous imports of torch torchvision and torchaudio with uv Learn how to use optional dependencies custom indexes and mutually exclusive extras for a robust setup

- Repository: [Th3Unknovvn/uv-install-torch](https://github.com/baonguyen6742/uv-install-torch)
- Tags: tutorial
- Published: 2026-02-26

---

**The baonguyen6742/uv-install-torch repository demonstrates a robust method to verify simultaneous imports of torch, torchvision, and torchaudio using uv's optional dependencies, custom indexes, and mutually exclusive extras.**

Managing PyTorch's ecosystem—`torch`, `torchvision`, and `torchaudio`—often requires navigating complex CPU and CUDA wheel configurations. The `uv-install-torch` repository streamlines this process by leveraging `uv`'s advanced dependency resolution to handle import verification for torch, torchvision, torchaudio simultaneously with uv while preventing conflicting installations.

## Configuring Optional Dependencies for CPU and CUDA

The foundation of this setup resides in [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) at lines 24–28, where the three packages are declared as **optional dependencies** grouped into extras named `cpu` and `cu124`.

```toml
[project.optional-dependencies]
cpu = ["torch==2.4.1", "torchvision==0.19.1", "torchaudio==2.4.1"]
cu124 = ["torch==2.4.1", "torchvision==0.19.1", "torchaudio==2.4.1"]

```

- The `cpu` extra pins CPU-only wheels from the standard PyTorch index.
- The `cu124` extra pins CUDA 12.4 wheels from the CUDA-specific index.
- Both extras use identical package versions (2.4.1, 0.19.1, 2.4.1), ensuring API compatibility across hardware targets.

## Configuring Custom Package Indexes

To fetch the correct wheels, [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) defines custom indexes in the `[tool.uv.sources]` section (lines 37–50) and declares them in the `[tool.uv]` block (lines 53–62).

```toml
[tool.uv.sources]
torch = [
  { index = "pytorch-cpu", extra = "cpu" },
  { index = "pytorch-cu124", extra = "cu124" },
]
torchvision = [
  { index = "pytorch-cpu", extra = "cpu" },
  { index = "pytorch-cu124", extra = "cu124" },
]
torchaudio = [
  { index = "pytorch-cpu", extra = "cpu" },
  { index = "pytorch-cu124", extra = "cu124" },
]

[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"

[[tool.uv.index]]
name = "pytorch-cu124"
url = "https://download.pytorch.org/whl/cu124"

```

This configuration ensures that when you install `.[cpu]`, `uv` pulls wheels exclusively from the `pytorch-cpu` index, while `.[cu124]` resolves against the `pytorch-cu124` index.

## Enforcing Mutually Exclusive Extras

To prevent ambiguous environments where both CPU and CUDA wheels might coexist, [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) at lines 30–33 declares the extras as **mutually exclusive**:

```toml
[tool.uv]
conflicts = [
  [{ extra = "cpu" }, { extra = "cu124" }],
]

```

Attempting to install both extras simultaneously—for example, `uv pip install .[cpu,cu124]`—triggers a resolution error. This safeguard ensures clean, deterministic environments for import verification.

## Verifying Imports with main.py

The repository includes [`main.py`](https://github.com/baonguyen6742/uv-install-torch/blob/main/main.py) to validate that all three packages import correctly and function as expected. The script imports `torch`, `torchvision`, and `torchaudio`, then prints version information, CUDA availability, and executes a tensor computation.

```python

# main.py

import torch
import torchvision
import torchaudio

def main():
    print("torch version      :", torch.__version__)
    print("torchvision version:", torchvision.__version__)
    print("torchaudio version :", torchaudio.__version__)
    print("CUDA available?    :", torch.cuda.is_available())
    
    # Verify tensor operations work

    device = "cuda" if torch.cuda.is_available() else "cpu"
    x = torch.arange(1, 3).unsqueeze(0).to(device)
    y = torch.arange(1, 4).unsqueeze(0).to(device)
    result = torch.matmul(x.T, y)
    print("Test calculation:")
    print(result)

if __name__ == "__main__":
    main()

```

If any import fails, Python raises an `ImportError` immediately, providing instant feedback that the installation requires troubleshooting.

## Installation and Verification Workflow

Execute the following commands to install the PyTorch stack and verify imports:

```bash

# Install CPU-only version

uv pip install .[cpu]

# OR install CUDA 12.4 version

uv pip install .[cu124]

# Run verification script using uv's managed environment

uv run python main.py

```

Successful execution produces output similar to:

```

torch version      : 2.4.1
torchvision version: 0.19.1
torchaudio version : 2.4.1
CUDA available?    : True
Test calculation:
tensor([[1, 2, 3],
        [2, 4, 6]])

```

## Summary

- The `baonguyen6742/uv-install-torch` repository uses [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) optional dependencies to define `cpu` and `cu124` extras at lines 24–28.
- Custom indexes in `[tool.uv.sources]` (lines 37–50) and `[[tool.uv.index]]` (lines 53–62) ensure `uv` fetches wheels from the correct PyTorch repository.
- Mutually exclusive extras declared at lines 30–33 prevent simultaneous installation of conflicting CPU and CUDA builds.
- The [`main.py`](https://github.com/baonguyen6742/uv-install-torch/blob/main/main.py) script provides immediate import verification and functional testing through version printing and tensor operations.

## Frequently Asked Questions

### How do I verify that torch, torchvision, and torchaudio are correctly installed with uv?

Run the [`main.py`](https://github.com/baonguyen6742/uv-install-torch/blob/main/main.py) script included in the repository using `uv run python main.py`. This script imports all three modules and prints their `__version__` attributes along with `torch.cuda.is_available()`. If any package fails to import, Python raises an `ImportError` immediately.

### Can I install both CPU and CUDA versions of PyTorch simultaneously using uv?

No. The [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) at lines 30–33 declares `cpu` and `cu124` as mutually exclusive extras. Attempting to install both with `uv pip install .[cpu,cu124]` triggers a conflict error, ensuring you maintain a single, coherent environment.

### What versions of PyTorch packages are pinned in the uv-install-torch repository?

According to lines 24–28 in [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml), the repository pins `torch==2.4.1`, `torchvision==0.19.1`, and `torchaudio==2.4.1` for both CPU and CUDA 12.4 variants. These versions are synchronized to ensure API compatibility across hardware configurations.

### How does uv know which PyTorch index to use for CPU vs CUDA wheels?

The `[tool.uv.sources]` section maps each package to specific indexes based on the active extra. When installing `.[cpu]`, `uv` uses the `pytorch-cpu` index (defined at lines 53–62); when installing `.[cu124]`, it uses the `pytorch-cu124` index. This conditional routing ensures the correct wheel variants are resolved without manual index management.