conda:列出所有使用某个包的环境 [英] Conda: list all environments that use a certain package

查看:276
本文介绍了conda:列出所有使用某个包的环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得使用 conda 中某个包的所有环境的列表?

How can one get a list of all environments that use a certain package in conda?

推荐答案

下面是如何使用 Conda Python 包(在 base 环境中运行)的示例:

Here's an example of how to do it with the Conda Python package (run this in base environment):

import conda.gateways.logging
from conda.core.envs_manager import list_all_known_prefixes
from conda.cli.main_list import list_packages
from conda.common.compat import text_type

# package to search for; this can be a regex
PKG_REGEX = "pymc3"

for prefix in list_all_known_prefixes():
    exitcode, output = list_packages(prefix, PKG_REGEX)
    
    # only print envs with results
    if exitcode == 0 and len(output) > 3:
        print('
'.join(map(text_type, output)))

这适用于 Conda v4.10.0,但由于它依赖于内部方法,因此无法保证继续前进.也许这应该是一个功能请求,比如像 conda list --any 这样的 CLI 命令.

This works as of Conda v4.10.0, but there since it relies on internal methods, there's no guarantee going forward. Perhaps this should be a feature request, say for a CLI command like conda list --any.

这是一个使用参数作为包名的版本:

Here is a version that uses arguments for package names:

conda-list-any.py

#!/usr/bin/env conda run -n base --no-capture-output python

## Usage: conda-list-any.py [PACKAGE ...]
## Example: conda-list-any.py numpy pandas

import conda.gateways.logging
from conda.core.envs_manager import list_all_known_prefixes
from conda.cli.main_list import list_packages
from conda.common.compat import text_type
import sys


for pkg in sys.argv[1:]:
    print("#"*80)
    print("# Checking for package '%s'..." % pkg)

    n = 0
    for prefix in list_all_known_prefixes():
        exitcode, output = list_packages(prefix, pkg)
        if exitcode == 0 and len(output) > 3:
            n += 1
            print("
" + "
".join(map(text_type, output)))

    print("
# Found %d environment%s with '%s'." % (n, "" if n == 1 else "s", pkg))
    print("#"*80 + "
")

顶部的 shebang 应该确保它可以在 base 中运行,至少在 Unix/Linux 系统上是这样.

The shebang at the top should ensure that it will run in base, at least on Unix/Linux systems.

这篇关于conda:列出所有使用某个包的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆