以下是如何使用 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)))
Run Code Online (Sandbox Code Playgroud)
这从 Conda v4.10.0 开始工作,但由于它依赖于内部方法,因此无法保证前进。也许这应该是一个功能请求,比如像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")
Run Code Online (Sandbox Code Playgroud)
顶部的 shebang 应确保它可以在base 中运行,至少在 Unix/Linux 系统上。
| 归档时间: |
|
| 查看次数: |
433 次 |
| 最近记录: |