(There are too many aspects in this question to cover it all at once. I will try to cover the initial points here, the rest should be asked in a new question.)
First, pip doesn't have an API. The preferred way of using pip from your own program is to call it directly as a subprocess.
The input format is nothing recognizable (not CSV, JSON, maybe YAML could cover it). But coincidentally the input format is somewhat readable as a literal Python list of strings, so one could try parsing the content with ast.literal_eval
.
pkgs.txt
:
{
"package1",
"package2",
}
main.py
#!/usr/bin/env python3
import ast
import pathlib
import subprocess
import sys
def main():
file_name = 'pkgs.txt'
file_path = pathlib.Path(file_name)
file_content = file_path.read_text()
pkgs = ast.literal_eval(file_content)
print("pkgs", pkgs)
for pkg in pkgs:
print("pkg", pkg)
command = [
sys.executable,
'-m',
'pip',
'search',
pkg,
]
print("command", command)
command_output = subprocess.check_output(command)
print("command_output")
print('"""')
print(command_output.decode())
print('"""')
if __name__ == '__main__':
main()
Extracting the correct information out of command_result
could be treated in a follow up question. (I suspect calling pip search
is not the best choice here, it might be worth going with one of PyPI's APIs directly instead.)
command_output
"""
package2 (0.0.0) -
101703383-python-package2 (0.0.3) - This is my second package in python.
Vigenere-cipher-package2 (0.5) - An example of Vigenere cipher
WuFeiLiuGuang-first-package2 (2.0.0) - test pkg ignore
"""
References: