Pip V22 install --report in Json to get requirements

I am using pip V22 to install a few packages and I would like to get from the json report (pip install --report -) just the version number and the requirements so as to keep this in a requirements file.

I am not familiar at all with json so please forgive me.
I tried json.loads(report_stdout) and I get error
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

the stdout comes from a subprocess. I tried utf-8 enconding or binary and I always ge the same error!..


For historical reasons, pip install logs to stdout. So you need to use --quiet when sending the json report to stout.

Thank you for you answer. I looked into the pip V22 documentation and it says that the option --report - sends the report to stdout:
Generate a JSON file describing what pip did to install the provided requirements. Can be used in combination with --dry-run and --ignore-installed to ‘resolve’ the requirements. When - is used as file name it writes to stdout.

from pip install - pip documentation v22.2.2

and I do not see the --quiet option. DO we talk about the same release?

Yes we do. I take note to improve the docs for the next release.

Very strange; if I do include --quiet --report - and --dry-run , I do get a json report but it contains only the report about the pip package, not the report about the installed packages or would be installed packages!..

That would be strange indeed. If you think there is a bug, please open an issue on the pip repo with a detailed reproducer.

Yes but I am using subprocess to call pip install from a python script. Is this a deviant use for pip?
Also I used the debug function of my IDE .
If you confirm this not a deviant use I will produce a reproducer

Invoking pip as a subprocess is perfectly legitimate.

Thanks for you help. No bug it was a misunderstanding from my side.

1 Like

Hi @piscvau, I developed a tool that is doing what you’re asking for I think: Using pip install --report to create requirements files: GitHub - sbaack/iso-freeze: Pin requirements using new `pip install --report`

You can check this file to see how I turned the report into JSON and extracted relevant information. Hope this helps.

yes exactlly this is what I was looking for Thank you so much.
I just looked into the idea of creating an sqlite database using inspect and sqlitebiter . but sqlitebiter does not like the output of inspect see here incompatibility between pip V22 json output and sqlitebiter · Issue #88 · thombashi/sqlitebiter · GitHub.
Anyhow your proposal is a very nice shortcut.
May I suggest that your tool be referenced into the pip documentation . It would have save me a lot of time. I did google to find an existing package but did not hit yours!..

Glad it’s useful! Not sure if this tool should be mentioned in the docs. At this point, it’s experimental and for a serious project people should use pip-tools which accomplishes the same without relying on pip install --report (or they could use tools like poetry or pdm with their own lockfile formats).

Somewhat related re: pip programmatic invocation