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

David Cournapeau cournape@gmail....
Wed Feb 3 03:41:54 CST 2010

On Wed, Feb 3, 2010 at 5:38 PM, Peter Notebaert <peno@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) {

        mod = PyImport_ImportModule("numpy");
        if (mod == NULL) {

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.



More information about the NumPy-Discussion mailing list