51

For a given conda package, how to I list the packages that depend on it?

I recently installed anaconda on a university cluster that already had a version of MPI (openmpi). The mpich2 package and mpi4py packages installed with anaconda were fine for demos of mpi4py, but the mpi* compilers (mpicc, etc) were not compatable. So I conda remove'd mpich2 and mpi4py and used pip to install mpi4py using the local MPI install and compilers.

I had to dig around to find mpi4py's dependencies and then mpich2's dependents, for which I only identified mpi4py. Is there an "easy" way to find out what depends on mpich2?

3
  • 2
    Is parsing your story about MPI necessary to understand your question, or does your opening sentence cover it?
    – duozmo
    Dec 16, 2015 at 16:32
  • 9
    @duozmo I'd guess the opening statement covers it, but I usually like to give context to my questions in case I'm not asking the right question.
    – Yann
    Dec 16, 2015 at 18:49
  • No objection to providing context. It's just a fairly involved scenario and I was spending a lot of time trying to figure out if we had the same question (your opening sentence).
    – duozmo
    Dec 16, 2015 at 19:29

9 Answers 9

45

conda info will tell you the directory (or directories) where your package cache is located. These directories contain a unique directory for each package, and each package directory contains an info directory and a file called index.json. There is a requires field in each of these files that refers to a list of conda dependencies. So in short, you need to search these files for the package you're trying to remove.

For example, if anaconda's installed in my home directory, and therefore the package cache is ~/anaconda/pkgs, to find mpich2's dependents, I would:

grep mpich2 ~/anaconda/pkgs/*/info/index.json

You will see 2 lines for the anaconda package, because mpich2 is both in the aforementioned requires list and in a list called depends. You'll also see one line for each mpich2 package available, because there is also a name field for each package. Then you'll see one or more lines for each package that depends on, requires mpich2. My search produced only mpi4py.

Now I thought you could do a --dry-run remove, but it appears that remove does not remove dependents, so nothing special is listed.

If grep is unavailable, then I'm sure you could make a python script to do the same thing, using say the globmodule and maybe even json to do the searching.

1
  • Useful tip about conda info! Also note - if you want to script something using this information, check out conda info --json. Mar 26, 2020 at 4:39
25

With recent versions of conda, you can do

conda remove --dry-run <package>

to get a list of packages that would be uninstalled along with the given one.

23

This has been solved in 2023, please read the new answer.


The old answer:

conda search --reverse-dependency <package>

should be the answer. Except it's not working. Please vote on this issue to show that it's important to users. It's been reported in Jan-18 and there has been no change in status. Hopefully if enough votes is gathered it'll be looked at. Or perhaps someone could submit a PR to fix it.

Until then you may have a partial solution using pipdeptree if pip's version of the package's reverse dependencies are identical to conda's version, which is often not the case. But at least it will give you some indication.

pipdeptree --reverse --packages <package>
13

Searching the package cache will only show you the packages that you have downloaded already. For your case, this behavior is fine, but if you want to know every package that depends on a given package, a better way is to search the repodata of your channels. The repodata is cached in ~/anaconda/pkgs/cache, or you can navigate with your browser to http://repo.continuum.io/pkgs/free/ and click on repodata.json for the platform you use (for Binstar, go to, e.g., https://conda.binstar.org/asmeurer). Then search for the name of the package in the "depends" key.

3
  • 4
    Would be nice to have this information easily accessible online. Jan 8, 2016 at 7:46
  • And now it is! :)
    – jared
    Mar 11, 2019 at 2:24
  • Great answer, most repos I've seen have also repodata.json.bz2 which downloads faster. The cache has almost the same data, except for a few keys in the JSON, like _url, _mod, _cache_control - If it wasn't for this information, I wouldn't know which channel this is for (the cache JSON filenames are some obscure hashes). Jan 28, 2020 at 15:13
11

mamba repoquery

While Mamba is primarily a drop-in replacement for Conda, one of the extra features it provides is solid functionality for dependency and reverse dependency querying through its repoquery command. However, note that this is only for environment-level relationships, so the packages must be installed and the environment activated.

Demo

Note that this is years out from the original question, so I'm just going demo with a mpi4py install and use the versions that get installed.

$ mamba create -n so-mpi4py mpi4py
## installs 24 packages

$ conda activate so-mpi4py
(so-mpi4py) $ mamba repoquery whoneeds mpich
                  __    __    __    __
                 /  \  /  \  /  \  /  \
                /    \/    \/    \/    \
███████████████/  /██/  /██/  /██/  /████████████████████████
              /  / \   / \   / \   / \  \____
             /  /   \_/   \_/   \_/   \    o \__,
            / _/                       \_____/  `
            |/
        ███╗   ███╗ █████╗ ███╗   ███╗██████╗  █████╗
        ████╗ ████║██╔══██╗████╗ ████║██╔══██╗██╔══██╗
        ██╔████╔██║███████║██╔████╔██║██████╔╝███████║
        ██║╚██╔╝██║██╔══██║██║╚██╔╝██║██╔══██╗██╔══██║
        ██║ ╚═╝ ██║██║  ██║██║ ╚═╝ ██║██████╔╝██║  ██║
        ╚═╝     ╚═╝╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝ ╚═╝  ╚═╝

        mamba (0.19.0) supported by @QuantStack

        GitHub:  https://github.com/mamba-org/mamba
        Twitter: https://twitter.com/QuantStack

█████████████████████████████████████████████████████████████


Executing the query mpich



 Name   Version Build           Depends              Channel           
────────────────────────────────────────────────────────────────────────
 mpi4py 3.1.3   py310hd348148_0 mpich >=3.4,<4.0.0a0 conda-forge/osx-64

Personally, I usually use the tree format, with

(so-mpi4py) $ mamba repoquery whoneeds --tree mpich

mpich[3.4.2]
  └─ mpi4py[3.1.3]

Real-World Example

More of a testimonial to its usefulness, I had noticed at some point that one of my R environments somehow ended up with Python installed. I'm very strict about this, so I reached for mamba repoquery whoneeds and found:

(bioc_3_12) host:dir usr$ mamba repoquery whoneeds -t python

Executing the query python

python[3.9.2]
  ├─ numpy[1.20.2]
  │  └─ colormath[3.0.0]
  │     └─ spectra[0.0.11]
  │        └─ r-rspectra[0.16_0]     # <- this package is the culprit!
  │           └─ r-uwot[0.1.10]
  ├─ networkx[2.5]
  │  └─ colormath already visited
  ├─ certifi[2021.5.30]
  │  └─ setuptools[49.6.0]
  │     ├─ networkx already visited
  │     └─ pip[21.0.1]
  ├─ python_abi[3.9]
  │  ├─ numpy already visited
  │  ├─ certifi already visited
  │  └─ setuptools already visited
  ├─ colormath already visited
  ├─ spectra already visited
  ├─ decorator[4.4.2]
  │  └─ networkx already visited
  ├─ wheel[0.36.2]
  │  └─ pip already visited
  ├─ pip already visited
  └─ setuptools already visited

Turned out the r-spectra package recipe in Conda Forge had misstated one of its dependencies to be a Python package (spectra) rather than a C++ dynamic library (spectralib).

0
7

There is a conda package for this: conda-tree. Documentation.

To find packages that depend on a package:

# which packages depend on a specific package
$ conda-tree whoneeds xz
['samtools', 'bcftools', 'htslib', 'python']

You could also dump the entire dependency tree, and then search:

# full dependency tree
$ conda-tree deptree --full
neovim==0.3.1
  ├─ pynvim 0.3.2 [required: any]
  │  ├─ greenlet 0.4.15 [required: any]
...
5

Sometime in 2023 conda finally added a working solution:

conda repoquery whoneeds

Example:

$ conda repoquery whoneeds -c conda-forge accelerate
Collecting package metadata: done
 Name                 Version     Build             Depends                Channel     Subdir  
────────────────────────────────────────────────────────────────────────────────────────────────
 autogluon.multimodal 0.6.2       py38h578d9bd_2    accelerate <0.14,>=0.9 conda-forge linux-64
 autogluon.multimodal 0.6.2       py38h578d9bd_3    conda-forge            linux-64   
 autogluon.multimodal 0.6.2       py38h578d9bd_4    conda-forge            linux-64   
[...]
 autogluon.multimodal 0.8.0       py39h4655687_1    conda-forge            linux-64
 autogluon.multimodal 0.8.0       py39h764a5bb_0    conda-forge            linux-64
 gradsflow            0.0.7.post2 pyhd8ed1ab_0      conda-forge            noarch
 gradsflow            0.0.8       pyhd8ed1ab_0      conda-forge            noarch
[...]
4

Shameless plug: conda-depgraph can do this fairly easily:

$ conda depgraph --from-channels in mpich2

 ┌────────┐
 │anaconda│
 └───┬─┬──┘
     │ │
     │ └──┐
     v    │
 ┌──────┐ │
 │mpi4py│ │
 └──┬───┘ │
    │ ┌───┘
    │ │
    v v
 ┌──────┐
 │mpich2│
 └──────┘
2

Based on Yann's answer, code diving in conda core and reading this I came up with the following script to get a reverse-dependency graph of all cached channels:

import glob
import json
import os

from collections import defaultdict

info = json.load(os.popen('conda info --json'))

print('Loading channels...')
channels = [
    json.load(open(repodata))
    for pkg_dir in info['pkgs_dirs']
    for repodata in glob.glob(os.path.join(pkg_dir, 'cache', '*.json'))
]
print('Done')

rdeps = defaultdict(set)

for c in channels:
    for k, v in c['packages'].items():
        package = '-'.join(k.split('-')[:-2])
        for dep in v['depends']:
            dependant = dep.split()[0]
            rdeps[dependant].add((package, c['_url']))

Now you can get the reverse dependencies of a specific package:

>>> print(rdeps['mpich2'])
set()

Well, right now nothing depends on it anymore... But if you run it in ipython or Jupyter, you can then quickly query the reverse dependencies, e.g.:

>>> print(rdeps['aiosqlite'])
{('databases', 'https://conda.anaconda.org/conda-forge/linux-64')}

You get the package name (without versions, of which there may be many), and the channel URL (which can show you, if it's pkgs/main/(arch) or conda-forge etc.

2
  • 1
    $ conda_rd () { python -c 'from reverse_dependency import rdeps; print(rdeps['$1'])' ; }
    – gboffi
    Jul 15, 2020 at 9:18
  • That's a nice bash function! We just need to make this module importable from everywhere. Jul 15, 2020 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.