我想知道哪些Python包是通过pip安装的,哪些是通过rpm安装的。
我在非任何虚拟环境中运行,并想知道是否有一些包是通过pip安装的。
背景:我们的政策是在“根级别”使用RPM。我想找到违反该政策的地方。
我在非任何虚拟环境中运行,并想知道是否有一些包是通过pip安装的。
背景:我们的政策是在“根级别”使用RPM。我想找到违反该政策的地方。
pip install --user <package_name>
以进行本地用户包安装。在debian系统上,默认情况下安装的软件包会被安装在:
/usr/lib/python2.7/dist-packages/
而pip安装的软件包会安装在以下位置:
/usr/bin/local/python2.7/dist-packages
import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
-l
将显示任何本地安装的软件包(即非全局软件包)。但是,您需要在正确的环境中。pip freeze -l
site-packages
目录。locate -r '/site-packages$'
pip freeze | awk '{split($0,a,"="); print a[1]}' | xargs -P 5 -I {} pip show {} | grep 'Name\|Location'
-l
和不带 -l
参数时的区别。可以很好地检测虚拟环境内外的差异,但它并不能帮助我们决定是从 RPM 还是从 pip 安装的。 - guettli/usr/lib/
与/usr/bin/local/
,还是相同的? - Luke Exton/usr/lib/python2.7/site-packages/
。 - guettliimport 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')
并将输出通过类似于以下内容的管道进行处理:
grep -e '-- NOT' -e '-- nothing could be determined'
pip list
返回所有已安装软件包的列表,则可以使用rpm -qa
查找通过rpm
安装的软件包,两者之间的差异将是不符合您策略的软件包。以上方法适用于您吗? - Jacques Gaudin$HOME/.cache/pip
或类似的根目录,但这不能保护你免受pip install x-1.2.3; apt-get install python-x-1.2.3
这样的序列攻击。 - Dima Tisnek