Skip to content

Instantly share code, notes, and snippets.

@zanieb
Created March 5, 2025 19:27
Show Gist options
  • Save zanieb/96b2825e6cec3bcc2b649aa02d43c40c to your computer and use it in GitHub Desktop.
Save zanieb/96b2825e6cec3bcc2b649aa02d43c40c to your computer and use it in GitHub Desktop.

Converting from requirements.in and requirements.txt to pyproject.toml and uv.lock

Use the following one-liner to reformat an existing requirements.txt file as constraints:

$ uv pip compile requirements.txt --no-annotate --no-header \
    | awk '{printf("%s\"%s\",\n", (NR==1?"constraint-dependencies = [\n    ":"    "), $0)}' \
    | sed '$s/,$/\n]/'

Then, add the constraints to the pyproject.toml in the [tool.uv] section:

TODO EXAMPLE

Then, add the input requirements as dependencies:

$ uv add -r requirements.in

Then, you can remove the constraints-dependencies entry from the pyproject.toml. These dependencies are now pinned in uv.lock instead.

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