4

I would like to do unit testing in the oldest version of Numpy which I can so I know whether or not the code base works in as many versions of Numpy as possible.

I am installing numpy through conda, and testing in several versions of Python. However, in conda different versions of Python each have different subsets of the numpy versions available to them.

Is there an easy way to find which versions of numpy are available on conda for a given version of python, and of these which is the lowest version?

scottclowe
  • 2,015
  • 19
  • 20
  • While my solution below does work for the problem stated here, I actually ended up needing something even more involved, which was to find the oldest version of numpy/scipy, constrained both by Python version **and** a minimum requirement on the package specification. This was rather more involved, but if you are interested [it is available as a shell script](https://github.com/scottclowe/python-continuous-integration/blob/master/continuous_integration/conda_min_version.sh), and you can use it (or [its repository](https://github.com/scottclowe/python-continuous-integration)) if you'd like to. – scottclowe Feb 10 '16 at 01:38

1 Answers1

3

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.

scottclowe
  • 2,015
  • 19
  • 20