简体   繁体   English

pip freeze:仅显示通过pip安装的软件包

[英]pip freeze: show only packages installed via pip

I want to know which python packages are installed via pip and which are installed via rpm. 我想知道哪些python软件包是通过pip安装的,哪些是通过rpm安装的。

I run outside any virtualenv, and want to know if there are some packages installed via pip. 任何virtualenv 外面运行,并想知道是否通过pip安装了一些软件包。

Background: Our policy is to use RPM at "root level". 背景:我们的政策是在“根级”使用RPM。 I want to find places where the policy was broken. 我想找到政策被打破的地方。

Assumptions: 假设:

  • I'm not sure about red-hat, but for debian/ubuntu. 我不确定红帽,但对于debian / ubuntu。
  • I'm assuming you are using system python. 我假设你正在使用系统python。
  • I don't think it matters, but you might have to check pip install --user <package_name> for local user package installs. 我认为这不重要,但您可能需要检查pip install --user <package_name>以进行本地用户包安装。

By default on debian system installed packages are installed at: 默认情况下,debian系统安装的软件包安装在:

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

And pip installed packages are installed at: pip安装包安装在:

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

To see all the installation paths you can run inside your python shell: 要查看可以在python shell中运行的所有安装路径:

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

As per the pip freeze docs -l will show you any local installs of packages (ie not global packages) However, you need to be in the correct environment. 根据pip freeze docs -l将显示任何本地安装的软件包(即非全局软件包)但是,您需要处于正确的环境中。

pip freeze -l

If Virtualenvs come into play: They will use site-packages directories. 如果Virtualenvs发挥作用:他们将使用site-packages目录。

locate -r '/site-packages$'

Also note any packages installed into a different directory will not be located at all via this method: Install a Python package into a different directory using pip? 另请注意,通过此方法根本不会找到安装到其他目​​录中的任何软件包: 使用pip将Python软件包安装到其他目​​录中?

Final trick , Check the exact install path in pip using pip show. 最后一招 ,使用pip show检查pip中的确切安装路径。 Effectively, get just the names from pip, pipe that into a pip show and filter the output for the Name -> Location map. 实际上,只需从pip获取名称,将其输入到pip show并过滤Name - > Location map的输出。

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

How about turn the question around slightly, and just check what belongs to rpms and what doesn't. 怎么样稍微转动问题,只检查什么属于rpms,什么不属于。 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM