Freeze delay: show only packages installed via pip

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

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

Reference Information. Our policy is to use root level RPM. I want to find places where the policy has been violated.

+5
source share
2 answers

How about turning the question a bit and just check what rpms belongs to 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 draw the output through something like

 grep -e '-- NOT' -e '-- nothing could be determined' 
+2
source

Assumptions:

  • I'm not sure about the red hat, but for debian / ubuntu.
  • I assume you are using system python.
  • I don't think this is important, but you might need to check pip install --user <package_name> for local package packages.

By default, packages are installed on the debian system:

/usr/lib/python2.7/dist-packages/

And installed packages are installed at:

/usr/bin/local/python2.7/dist-packages

To view all installation paths that you can run inside the python shell:

 import site; site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] 

According to pip freeze, docs -l will show you any local package installations (i.e. not global packages). However, you need to be in the right environment.

 pip freeze -l 

If Virtualenvs come into play: They will use site-packages directories.

 locate -r '/site-packages$' 

Also note that any packages installed in another directory will not be found at all using this method: Install a Python package in another directory using pip?

Final trick . Check the exact installation path in pip using show pip. In fact, get only the names from pip, pipe that appear in show pip, and filter the output for the Name β†’ Location map.

 pip freeze | awk '{split($0,a,"="); print a[1]}' | xargs -P 5 -I {} pip show {} | grep 'Name\|Location' 
+1
source

All Articles