10

How to list package versions available with conda has useful answers, one of which is at https://stackoverflow.com/a/47795843/257924 which uses two equal signs. conda search -h doesn't state fully what MatchSpec syntax allows, and only gives a spartan number of examples.

For example, I wanted to see what packages under the latest version of python exist, for a package called jedi. I had to resort to experimentation and guessing to find the right syntax, because of the above lack in detailed documentation of the MatchSpec syntax. I ended up with:

$ condaw search 'jedi[build=py37*]' --json | grep '"build"' 
      "build": "py37_1",
      "build": "py37_0",
      "build": "py37_0",
      "build": "py37_0",
      "build": "py37_0",
$ 

The above --json option was used just so that I could find out what keywords such as build might be a part of the syntax.

So, where is the MatchSpec syntax officially and fully documented so that I don't have to guess? I'm concluding for now that -h output is the only one.

Jay Walker
  • 4,654
  • 5
  • 47
  • 53
bgoodr
  • 2,744
  • 1
  • 30
  • 51
  • Just now seeing this was closed as off-topic. Why can't it be moved into a stackexchange forum whereby it is on topic instead of closing it? – bgoodr Feb 18 '20 at 17:02
  • There aren't ways to migrate any question to any stack site, but apart from that It looks like your question is about where the documentation is located, that's not on topic on any stack as far as I know. – Captain Man Feb 18 '20 at 17:28
  • 2
    @CaptainMan Documentation is one of the most important aspects of software. Could you point us to some manifesto for stackoverflow that spells out why this question would be out of scope? – bgoodr Feb 20 '20 at 19:53
  • 1
    Maybe I'm wrong, maybe it is on topic, but I spoke poorly. A better thing to have said would be "This is more likely to be on topic if you ask what the exact syntax of MatchSpec is rather than asking where the docs are" and I should've helped by suggesting an edit to do that, as Jay Walker did. My apologies. :) *(And, I swear I don't mean for this to be cheeky, but the [tour page](https://stackoverflow.com/tour) says what is and isn't on topic. Asking where docs are seems like a gray area.)* – Captain Man Feb 20 '20 at 20:01

1 Answers1

15

There is some (possibly outdated) documentation in the conda-build docs. However, I still regard the documentation in their code as clearer. It can be read from an activated base env using

python -c "from conda.models.match_spec import MatchSpec; help(MatchSpec)"

or on the GitHub repo.

Technically, the code documentation is more about serializing the MatchSpec object to the string representation, rather than the other direction of parsing the string, but it covers all the possibilities.


MatchSpec String

Part of the docs describe the class itself, but here are the relevant parts on the string literal representation of the class:

The canonical string representation can generically be represented by

(channel(/subdir):(namespace):)name(version(build))[key1=value1,key2=value2]

where () indicate optional fields. The rules for constructing a canonical string representation are:

  1. name (i.e. "package name") is required, but its value can be '*'. Its position is always outside the key-value brackets.
  2. If version is an exact version, it goes outside the key-value brackets and is prepended by ==. If version is a "fuzzy" value (e.g. 1.11.*), it goes outside the key-value brackets with the .* left off and is prepended by =. Otherwise version is included inside key-value brackets.
  3. If version is an exact version, and build is an exact value, build goes outside key-value brackets prepended by a =. Otherwise, build goes inside key-value brackets. build_string is an alias for build.
  4. The namespace position is being held for a future conda feature.
  5. If channel is included and is an exact value, a :: separator is ued between channel and name. channel can either be a canonical channel name or a channel url. In the canonical string representation, the canonical channel name will always be used.
  6. If channel is an exact value and subdir is an exact value, subdir is appended to channel with a / separator. Otherwise, subdir is included in the key-value brackets.
  7. Key-value brackets can be delimited by comma, space, or comma+space. Value can optionally be wrapped in single or double quotes, but must be wrapped if value contains a comma, space, or equal sign. The canonical format uses comma delimiters and single quotes.
  8. When constructing a :class:MatchSpec instance from a string, any key-value pair given inside the key-value brackets overrides any matching parameter given outside the brackets.

Supported Keys

In addition to the fields that are explicitly represented in the string, the following keys are supported:

  • build_number
  • track_features
  • features
  • url
  • md5
  • license
  • license_family
  • fn

Note: fn stands for filename.

Examples

The documentation continues on to give examples, showing how one can use this MatchSpec class to generate these canonical strings:

>>> str(MatchSpec(name='foo', build='py2*', channel='conda-forge'))
'conda-forge::foo[build=py2*]'

>>> str(MatchSpec('foo 1.0 py27_0'))
'foo==1.0=py27_0'

>>> str(MatchSpec('foo=1.0=py27_0'))
'foo==1.0=py27_0'

>>> str(MatchSpec('conda-forge::foo[version=1.0.*]'))
'conda-forge::foo=1.0'

>>> str(MatchSpec('conda-forge/linux-64::foo>=1.0'))
"conda-forge/linux-64::foo[version='>=1.0']"

>>> str(MatchSpec('*/linux-64::foo>=1.0'))
"foo[subdir=linux-64,version='>=1.0']"
merv
  • 67,214
  • 13
  • 180
  • 245
  • 1
    Fantastic answer. And it's nice that there is a way to dump the help from the command line as well! – bgoodr Sep 01 '19 at 15:59
  • Is it possible to search by Python `version` only, for example: `conda search "numpy=py27_0" --info`? – Jelphy Aug 01 '22 at 13:33
  • @Jelphy yes, and good question. Please create it as a new question and would be happy to answer. Feel free to ping me with the link. – merv Aug 01 '22 at 17:03
  • 1
    @merv https://stackoverflow.com/questions/73197544/conda-search-for-package-specify-python-version – Jelphy Aug 01 '22 at 17:36