Skip to content

"numpy.core.umath failed to import" with python3 #214

@uwydoc

Description

@uwydoc

i know #209 has been considered to be an arch-related build problem and thus marked as closed. however, i encountered a very similar import error, with no "endianness error" reported, just ImportError. numpy.core.umath failed to import.

base code:

#include <boost/python.hpp>
#include <boost/python/numpy.hpp>

void hello() { PySys_WriteStdout("hello\n"); }

BOOST_PYTHON_MODULE(hello) {
  boost::python::numpy::initialize();
  boost::python::def("hello", hello);
}

CMakeLists.txt

find_package(PythonLibs 3)
find_package(Boost COMPONENTS python3 numpy3)

PYTHON_ADD_MODULE(hello hello.cpp)
target_include_directories(hello PRIVATE ${PYTHON_INCLUDE_DIR})
target_link_libraries(hello Boost::boost Boost::python3 Boost::numpy3 ${PYTHON_LIBRARIES})

my build env:

os: ubuntu 16.04 x86_64
cc: clang 3.8.0 (NOTE: installed with 'apt install clang')
python: 3.5.2 (NOTE: lib with 'm' suffix, installed with 'apt install python3 python3-dev')
numpy: 1.14.5 (NOTE: installed with 'python3 -m pip install --user numpy')
boost build cmd:
  ./bootstrap.sh --with-toolset=clang --with-python=python3
  ./b2 toolset=clang link=static cxxflags="-std=c++11 -fPIC" variant=release --with-python

after some tweaking, i found the following workarounds (modify libs/python/src/numpy/numpy.cpp)

  1. swap the order of wrap_import_array() and import_ufunc()
  2. or replace wrap_import_array() with _import_array()
  3. or add return NULL; after import_array() in wrap_import_array implementation for py3

i googled around the web, with no similar result. wierd. hope this workaround would help someone else.


another interesting thing is, if i call wrap_import_array and import_ufunc directly in my own module .c file, as follows:

#include <boost/python.hpp>
#include <numpy/arrayobject.h>
#include <numpy/ufuncobject.h>

static void * wrap_import_array() { import_array(); }

void hello() { PySys_WriteStdout("hello\n"); }

BOOST_PYTHON_MODULE(hello) {
  wrap_import_array();
  import_ufunc();

  boost::python::def("hello", hello);
}

program crashed with Illegal instruction message when import hello, stack trace contains no helpful message (i think); however, if i call np::initialize() instead, the program would not crash, but here comes the ImportError. numpy.core.umath failed to import error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions