Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pip freeze: show only packages installed via pip

Tags:

python

pip

I want to know which python packages are installed via pip and which are installed via rpm.

I run outside any virtualenv, and want to know if there are some packages installed via pip.

Background: Our policy is to use RPM at "root level". I want to find places where the policy was broken.

like image 610
guettli Avatar asked May 26 '16 13:05

guettli


People also ask

How do you see all packages that I pip installed?

If you want to list all the Python packages installed in an environment, pip list command is what you are looking for. The command will return all the packages installed, along with their specific version and location.

Does pip freeze show dependencies?

As I said above, running pip freeze gives you a list of all currently installed dependencies, but that does mean all of them.


1 Answers

How about turn the question around slightly, and just check what belongs to rpms and what doesn't. Try:

import os, sys, subprocess, glob

def type_printed(pth, rpm_dirs=False):

    if not os.path.exists(pth):
        print(pth + ' -- does not exist')
        return True        
    FNULL = open(os.devnull, 'w')
    if rpm_dirs or not os.path.isdir(pth):
        rc = subprocess.call(['rpm', '-qf', pth], stdout=FNULL, stderr=subprocess.STDOUT) 
        if rc == 0:
            print(pth + ' -- IS RPM')
            return True 
        print(pth + ' -- NOT an RPM')
        return True 
    return False 


for pth in sys.path:
    if type_printed(pth):
        continue 
    contents = glob.glob(pth + '/*') 
    for subpth in contents:
        if type_printed(subpth, rpm_dirs=True):
            continue
        print(subpth + ' -- nothing could be determined for sure')

And pipe the output through something like

grep -e '-- NOT' -e '-- nothing could be determined'
like image 195
daphtdazz Avatar answered Sep 28 '22 17:09

daphtdazz