Conda:列出使用特定程序包的所有环境 [英] Conda: list all environments that use a certain package

查看:93
本文介绍了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('\n'.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("\n" + "\n".join(map(text_type, output)))

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

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

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

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

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