uv icon indicating copy to clipboard operation
uv copied to clipboard

Include a method to create machine readable output

Open pfmoore opened this issue 10 months ago • 3 comments

When automating uv, having to parse the command output (which is intended for human readability) is not ideal. It would be useful to have an option to produce machine readable (probably JSON) output from the various uv commands.

  • For uv pip install something along the lines of the pip installation report would probably be a reasonable format.
  • For uv pip list, the pip list --format=json command is probably a good model.
  • uv pip show and uv pip freeze are machine readable, although having a --format=json equivalent for consistency might be worthwhile.
  • uv pip check probably doesn't need an output option, assuming the exit code is success or failure as appropriate.
  • uv pip compile and uv pip sync can probably have an installation report similar to uv pip install. I've not used these myself, though, so I don't know how useful they would be in practice.
  • uv venv could produce JSON output showing details of the created environment - path to the environment, Python executable path, scripts and site-package directories. Basically, the values that the stdlib venv module captures in the "context" object documented here.

To make automation easier, it's probably worth having some additional command line capabilities:

  • An isolated mode, to prevent user config affecting results (uv doesn't have as many possibilities for problems here as pip does, but that may change, I guess...)
  • A "guaranteed quiet" mode that ensures that just the JSON data will be printed to stdout, with any user messages either going to stderr or being suppressed. (Writing the data to a file is another possibility, but IMO it tends to be clumsy to manage).
  • I assume uv always writes its output as UTF-8, but if it doesn't (for example it follows the system defined encoding) then a "guaranteed UTF-8 output" mode would be important to avoid encoding problems.

pfmoore avatar Apr 22 '24 21:04 pfmoore

should we note in the description that uv pip list --format json already exists? https://github.com/astral-sh/uv/blob/main/crates/uv/src/commands/pip_list.rs#L115

$ python -m uv pip list --format json | jq .
[
  {
    "name": "certifi",
    "version": "2024.2.2"
  },
  {
    "name": "charset-normalizer",
    "version": "3.3.2"
  },
  {
    "name": "idna",
    "version": "3.6"
  },
  {
    "name": "requests",
    "version": "2.31.0"
  },
  {
    "name": "urllib3",
    "version": "2.2.1"
  }
]

ChannyClaus avatar Apr 24 '24 03:04 ChannyClaus

Thanks, I’d missed that.

pfmoore avatar Apr 24 '24 08:04 pfmoore

We also need it for equivalent of --dry-run without doing the install.

Related:

  • https://github.com/astral-sh/uv/issues/411
  • https://github.com/astral-sh/uv/issues/1442

sfc-gh-jcarroll avatar Apr 25 '24 12:04 sfc-gh-jcarroll