
On Wed, Feb 3, 2010 at 03:41, David Cournapeau <cournape@gmail.com> wrote:
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) { return; }
mod = PyImport_ImportModule("numpy"); if (mod == NULL) { return; } Py_DECREF(mod);
Or rather, to recover from the failed import as the OP wants to do: mod = PyImport_ImportModule("numpy"); if (mod == NULL) { /* Clear the error state since we are handling the error. */ PyErr_Clear(); /* ... set up for the sans-numpy case. */ } else { Py_DECREF(mod); import_array(); /* ... set up for the with-numpy case. */ } -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco