Search code examples
pythonlisttuples

Parse output of pip list / pip freeze in python


Hello I have a string like this:

AdvancedHTMLParser (8.0.1)\nappdirs (1.4.3)\nbeautifulsoup4 (4.6.0)\nchardet (3.0.4)\nchrome-gnome-shell (0.0.0)\ncupshelpers (1.0)\ncycler (0.10.0)\nCython (0.27.3)

I want to split this in a list of tuples. So that each list items has a tuple with two values, the name and the version (without the brackets).

I was only able to split the string by newline but I don't know how to properly grab the numbers in the brackets etc Can someone explain me how I can do this?

EDIT : I am trying to parse pip list local

 def get_installed_modules(self):
    data = subprocess.check_output(["pip", "list", "--local"])
    result = [tuple(line.replace('(', '').replace(')', '').split())
              for line in data.splitlines()]
    print(result)

I have the project that I cant just split the string but it requires a byte like object...

TypeError: a bytes-like object is required, not 'str'

Solution

  • Option 1
    If you're getting these outputs from pip, you can do it programmatically, using pip.operations.freeze -

    from pip.operations import freeze  
    
    modules = list(
        map(lambda x: x.split('=='), freeze.freeze(local_only=True))
    )
    
    print(modules)
    
    [['aiodns', '1.1.1'],
     ['aiohttp', '1.2.0'],
     ['appdirs', '1.4.0'],
     ['appnope', '0.1.0'],
     ['argparse', '1.4.0'],
    ...
    

    Option 2
    You could also use get_installed_distributions, taken from here:

    import pip
    
    modules = []
    for i in pip.utils.get_installed_distributions():
        modules.append((i.key, i.version))
    
    print(modules)
    
    [('pytreebank', '0.2.4'),
     ('cssselect', '1.0.1'),
     ('numba', '0.36.0.dev0+92.g2818dc9e2'),
     ('llvmlite', '0.0.0'),
     ('yarl', '0.8.1'),
     ('xlwt', '1.3.0'),
     ('xlrd', '1.1.0'),
     ...
    ]
    

    Option 3
    A third method is using pip.main -

    import pip
    pip.main(['list', 'local'])
    

    However, this writes to stdout.