❯ uv sync --dry-run
Discovered existing environment at: .venv
Resolved 107 packages in 0.89ms
Found up-to-date lockfile at: uv.lock
Would uninstall 2 packages
Would install 1 package
torch==2.7.1+cu126
torch==2.7.1
torch==2.7.1+cu126
I have this issue, every time I try to run anything using uv run, for some reason, it uninstall and reinstall pytorch, how can I update pyproject.toml or the uv.lock so that it doesn't happen anymore ?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To resolve the issue where uv sync repeatedly reinstalls PyTorch, follow these steps to align your pyproject.toml and lockfile:
1. Explicitly Specify PyTorch Version in pyproject.toml
Ensure the PyTorch version includes the CUDA variant to prevent ambiguity. Update the dependency to exactly match the desired version (e.g., 2.7.1+cu126):
toml
[project]
dependencies = [
"torch==2.7.1+cu126", # Use the exact version with CUDA specifier
]
2. Regenerate the Lockfile
Delete the existing lockfile (uv.lock) and regenerate it to reflect the explicit version:
bash
rm uv.lock
uv lock # Generates a new lockfile
3. Recreate the Virtual Environment
A broken environment state might be causing conflicts. Recreate it from scratch:
bash
rm -rf .venv # Delete the existing environment
uv venv # Create a new virtual environment
uv sync # Sync with the new lockfile
Why This Happens
PyTorch uses platform-specific builds (e.g., CPU vs. CUDA). Without an explicit version pin, the resolver may flip-flop between variants.
Your lockfile/environment likely contained both torch==2.7.1 (generic) and torch==2.7.1+cu126 (CUDA), causing conflicts during sync.
Verify the Fix
After these steps, run:
bash
uv sync --dry-run
It should show no changes (or expected changes unrelated to PyTorch).
Alternative: Use PyTorch's Official Index (Optional)
If version pinning doesn't work, specify PyTorch's repository in pyproject.toml:
toml
[[tool.uv.sources]]
name = "torch"
type = "index"
url = "https://download.pytorch.org/whl/cu126" # CUDA 12.1
[project]
dependencies = [
"torch==2.7.1", # Now resolves to the CUDA build from the custom index
]
Regenerate the lockfile and environment as above.
Key Takeaways
Always pin PyTorch to the full version string (e.g., 2.7.1+cu126).
Regenerate lockfiles after dependency changes.
Recreate environments to resolve inconsistent states.
This ensures uv sync recognizes the correct PyTorch variant and stops unnecessary reinstalls.