Skip to content

Instantly share code, notes, and snippets.

@yhoiseth
Last active May 11, 2025 18:26
Show Gist options
  • Save yhoiseth/c80c1e44a7036307e424fce616eed25e to your computer and use it in GitHub Desktop.
Save yhoiseth/c80c1e44a7036307e424fce616eed25e to your computer and use it in GitHub Desktop.
Upgrade all dependencies to their latest version using uv
#!/usr/bin/env python
# https://gist.github.com/yhoiseth/c80c1e44a7036307e424fce616eed25e
from typing import Any
from re import match, Match
import toml
import subprocess
def main() -> None:
with open("pyproject.toml", "r") as file:
pyproject: dict[str, Any] = toml.load(file)
dependencies: list[str] = pyproject["project"]["dependencies"]
package_name_pattern = r"^[a-zA-Z0-9\-]+"
for dependency in dependencies:
package_match = match(package_name_pattern, dependency)
assert isinstance(package_match, Match)
package = package_match.group(0)
uv("remove", package)
uv("add", package)
def uv(command: str, package: str) -> None:
subprocess.run(["uv", command, package])
if __name__ == "__main__":
main()
@gbaian10
Copy link

Thank you @gbaian10, your script worked perfectly for me.

Perhaps you can consider adding the requirements at the top to make it possible to run without a venv with uv:

#! /usr/bin/env -S uv run --script
#
# Source:
# https://gist.github.com/yhoiseth/c80c1e44a7036307e424fce616eed25e?permalink_comment_id=5387642#gistcomment-5387642
#
# /// script
# requires-python = ">=3.12"
# dependencies = [
#   "typer>=0.15.3",
#   "tomlkit>=0.13.2",
# ]
# ///

Thanks. I think this script is still quite rough — it mostly just handles cases where your package uses >= or ==.
Later, I realized it doesn't account for situations without any symbol (with use [tool.uv.sources]), and maybe such cases should also be skipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment