conda:列出使用特定软件包的所有环境

hgqdbh6s  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(344)

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

vzgqcmou

vzgqcmou1#

下面是一个如何使用conda python包(在基本环境中运行此程序)执行此操作的示例:

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开始,它就可以工作,但由于它依赖于内部方法,因此无法保证继续工作。也许这应该是一个功能请求,比如对于像这样的cli命令 conda list --any .

脚本版本

以下是使用包名称参数的版本:
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应该确保它将在base中运行,至少在unix/linux系统上运行。

uujelgoq

uujelgoq2#

我也遇到了同样的问题,并构建了一个powershell脚本:
将正则表达式查询作为参数
浏览所有可用的conda环境(并在执行此操作时显示进度条)
列出每个环境中的所有包
返回包含与查询匹配的包的环境
按返回的环境显示与查询匹配的包名称
该脚本位于此答案的底部。我已将其保存在一个文件夹中,该文件夹在路径中的名称下可用 search-pkg-env.ps1 .
脚本可以按如下方式运行(在这里,我搜索具有 pip 包裹 pymupdf ):

PS > search-pkg-env.ps1 pymupdf
Searching 19 conda environments...
py39
 - pymupdf 1.18.13

剧本

if ($args.Length -eq 0) {
  Write-Host "Please specify a search term for that matches the package name (regex)." -BackgroundColor DarkYellow -ForegroundColor White
    exit
}

$pkg_regex = $args[0]
$conda_envs = ConvertFrom-JSON -InputObject $([string]$(conda env list --json))

$total = $conda_envs.envs.Count
Write-Host "Searching $total conda environments..."
$counter = 1

ForEach ($conda_env in $conda_envs.envs) {
  if ($total -gt 0) {
      $progress = $counter / $total * 100
  }
  else {
      $progress = 0
  }
  # Split the full path into its elements
  $parts = $conda_env -Split '\\'
  # The last element has the env name
  $env_name = $parts[-1]
  Write-Progress -Activity "Searching conda environment $env_name for $pkg_regex" -PercentComplete $progress
  # Now search the provided package name in this environment
  $search_results = ConvertFrom-JSON -InputObject $([string]$(conda list $pkg_regex -n $env_name --json))
  If ($search_results.Count -gt 0) {
    Write-Host $env_name
      foreach ($result in $search_results) {
        $pkg_name = $result.name
        $pkg_version = $result.version
        Write-Host " - $pkg_name $pkg_version"
      }
  }
  $counter++
}

相关问题