First, we need to get the full list of versions
Let's say we want to get the earliest available version of numpy under python 3.2, so we can set the parameters like so.
PACKAGE=numpy
PYTHON_VERSION=3.2
We can solve the problem by getting the canonical set of search results which have this package as their full name conda search --canonical -f "$PACKAGE"
, and using sed
to search for the packages which have the right python version number. The canonical package names follow the format name-version-buildstring, and buildstring should contain the python version somewhere, though in the format as py32 for version 3.2, etc. Consequently we need to search for py${PYTHON_VERSION/./}
, where the .
in the version number has been removed.
The full search term should be ^$PACKAGE-\([^-]*\)-.*py${PYTHON_VERSION/./}.*
, and we are interested in the version number, which is the captured group.
As well as extracting the version number, we need to remove the lines which are not matched by the search, which is done here with the -n
at the start and p
at the end of the sed
command.
With the following line of code, we can get the full list of versions of a package available for a given python version:
conda search --canonical -f "$PACKAGE" | \
sed -n "s/^$PACKAGE-\([^-]*\)-.*py${PYTHON_VERSION/./}.*/\1/p" | \
sort -Vu
If you don't have the -V flag available for sort, here is an alternative solution. That said, the list provided by conda search should be sorted by version number anyway, and the -V
flag is there just in case. The -u
flag is needed to make sure the output is unique, since there may be multiple builds with the same package version and python version.
As the outputted list is sorted by version number in ascending order, the first entry is the lowest available version and can be taken with head -1
.
conda search --canonical -f "$PACKAGE" | \
sed -n "s/^$PACKAGE-\([^-]*\)-.*py${PYTHON_VERSION/./}.*/\1/p" | \
sort -Vu | \
head -1
And we're done. This should work for any PACKAGE and PYTHON_VERSION combination.
Note:
Conda does warn that the build string is subject to change and should net be relied upon to extract meaningful information. Consequently, this solution may break in the future.