[Numpy-discussion] Determine if numpy is installed from an extension

David Cournapeau cournape at gmail.com
Wed Feb 3 04:41:54 EST 2010


On Wed, Feb 3, 2010 at 5:38 PM, Peter Notebaert <peno at telenet.be> wrote:
> >From an extension? How to import numpy from there and then test if that
> succeeded and that without any annoying message if possible...

One obvious solution would be to simply call PyImport_Import, something like:

#include <Python.h>

PyMODINIT_FUNC initfoo(void)
{
        PyObject *m, *mod;

        m = Py_InitModule("foo", NULL);
        if (m == NULL) {
                return;
        }

        mod = PyImport_ImportModule("numpy");
        if (mod == NULL) {
                return;
        }
        Py_DECREF(mod);
}

But I am not sure whether it would cause some issues if you do this
and then import the numpy C API (which is mandatory before using any C
functions from numpy). I know the python import system has some dark
areas, I don't know if that's one of them or not.

cheers,

David



More information about the NumPy-Discussion mailing list