-1
Traceback (most recent call last):                                                                                                                    
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/__init__.py", line 17, in <module>                                                          
    from . import multiarray                                                                                                                          
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/multiarray.py", line 14, in <module>                                                        
    from . import overrides                                                                                                                           
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/overrides.py", line 7, in <module>                                                          
    from numpy.core._multiarray_umath import (                                                                                                        
ImportError: /usr/local/lib/python3.5/dist-packages/numpy/core/_multiarray_umath.cpython-35m-x86_64-linux-gnu.so: undefined symbol: PyExc_RecursionError                                                                                                                                                    

During handling of the above exception, another exception occurred:                                                                                   

Traceback (most recent call last):                                                                                                                    
  File "<string>", line 1, in <module>                                                                                                                
  File "/usr/local/lib/python3.5/dist-packages/numpy/__init__.py", line 142, in <module>                                                              
    from . import core
  File "/usr/local/lib/python3.5/dist-packages/numpy/core/__init__.py", line 47, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.5 from "/usr/bin/python3",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.17.4" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.

Original error was: /usr/local/lib/python3.5/dist-packages/numpy/core/_multiarray_umath.cpython-35m-x86_64-linux-gnu.so: undefined symbol: PyExc_RecursionError

Getting this error on running Python code through C++ using cpython api. The python file simply imports numpy. Does anyone knows what could be the possible issue and its solution?

I tried multiple different python installations by compiling them from source, but that seems to have no effect. Currently I have only python3.5 and python3.5m installed on the system. These are the default python installations that came with the system (ubuntu 16.04).

Below is the cmake file:

cmake_minimum_required(VERSION 2.8)
project(testproject C CXX)


set (CMAKE_CXX_STANDARD 14)
set(CMAKE_VERBOSE_MAKEFILE OFF)

set(CMAKE_JS_INC "$ENV{HOME}/.cmake-js/node-x64/v10.15.3/include/node;/home/project/code/node_modules/nan")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Release)

#------ PACKAGES ------
find_package(OpenCV 3 REQUIRED)
#---------------------------


#------ CORE ----
file(GLOB SOURCE_FILES "src/*.cpp" "src/*.h" "nan/*.cpp" "nan/*.h"
    "drivers/*.cpp" "drivers/*.h"
    "drivers/webstream/*.cpp" "drivers/webstream/*.h"
    )

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC}
    PUBLIC "."
    )

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
#---------------------------


#------ EVAL PACKAGES ------
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
#---------------------------
# c-python config
include_directories(${PROJECT_NAME} SYSTEM PUBLIC "/usr/include/python3.5/")
include_directories(${PROJECT_NAME} SYSTEM PUBLIC "/usr/lib/python3.5/")
include_directories(${PROJECT_NAME} SYSTEM PUBLIC "/usr/local/lib/python3.5/")
include_directories(${PROJECT_NAME} SYSTEM PUBLIC "/usr/local/lib/python3.5/dist-packages/numpy/core/include/")
add_library(python_c SHARED IMPORTED)
set_target_properties(python_c PROPERTIES IMPORTED_LOCATION "/usr/lib/x86_64-linux-gnu/libpython3.5m.so")

target_link_libraries(
        ${PROJECT_NAME}
        PUBLIC python_c
)


#add_executable
add_executable(code pawan.cpp)
target_link_libraries(code
        PUBLIC python_c
        PUBLIC ${OpenCV_LIBS})

Command for cmake is:

cmake -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3.5 ..

Through cmake, I also created an executable file "code". The executable is working fine. The error comes when using the code through library created through cmake.

This is the file pawan.cpp

#include "python3.5/Python.h"
#include <iostream>
#include <string>

using namespace std;

void fun() {
    cout << "using cpython......................................................................00" << endl;
    Py_Initialize();
    PyRun_SimpleString ("print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^11')");
//    if(PyArray_API == NULL){
//        _import_array();
//    }
    PyRun_SimpleString ("import numpy; print(numpy.__file__)");

    PyRun_SimpleString ("print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^22')");
}
Pawan
  • 1
  • 1
  • 3
  • Welcome to Stackoverflow! Please provide a description of how you already tried to solve the problem. Did you try all of the suggestions listed in the error message? – Kevin Nov 20 '19 at 14:28
  • @squareskittles updated the question as you suggested. – Pawan Nov 20 '19 at 14:33
  • A PyExc_RecursionError means a function (perhaps `import` in this case) calls itself, and does so "too many" times. Do you import numpy independently with pybind11 (`pybind11::module::import("numpy")`) before running other code? If not, do you call numpy's initialization (`import_array();`)? – Wim Lavrijsen Nov 20 '19 at 17:26
  • Could it be that you named you file `numpy.py` too? – arrowd Nov 21 '19 at 06:40
  • Both of the above are giving the same error. @WimLavrijsen – Pawan Nov 21 '19 at 07:32
  • If those lines already fail, can you post the minimal pybind11 code that shows the problem so that we can try to reproduce it? – Wim Lavrijsen Nov 21 '19 at 07:35
  • @WimLavrijsen Updated the question. Added the cmake file. – Pawan Nov 21 '19 at 12:10
  • @arrowd I haven't named the file numpy.py – Pawan Nov 21 '19 at 12:10

1 Answers1

0

Thanks for the full cmake file. In there, the application that you build includes and links with Python3.5. The error from numpy, however, comes from _multiarray_umath.cpython-38-x86_64-linux-gnu.so, so was build for Python3.8, and presumably it is picked up b/c it is the version of Python currently in your environment. C-extensions build for different versions of CPython are generally incompatible.

Unless you have a specific need for Python3.5, in which case I'd recommend to make sure it's setup properly in your environment (ie. by pointing PATH and LD_LIBRARY_PATH to the Python3.5 versions) and has numpy installed first. But if 3.8 will do, you can simply let cmake pick it up from the environment. Something like this:

find_package(PythonInterp)

if (PYTHONINTERP_FOUND)
   execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys;sys.stdout.write(sys.version[:3])"
                   OUTPUT_VARIABLE PYTHON_VERSION)
   execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys;sys.stdout.write(sys.prefix)"
                   OUTPUT_VARIABLE PYTHON_PREFIX)
   set (CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${PYTHON_PREFIX})
endif()

set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION})
find_package(PythonLibs REQUIRED)

if (NOT PYTHONLIBS_FOUND)
   message(FATAL_ERROR "PythonLibs package not found and python component required")
endif()

And add "${PYTHON_INCLUDE_PATH}" to your project includes and "{$PythonLibs}" to link your target with.

Wim Lavrijsen
  • 3,453
  • 1
  • 9
  • 21
  • Sorry for the trouble. I have updated the question. Previously I added error that came when i was trying with python3.8 and the cmakelists.txt file I added later and at that time, I was trying python3.5. I have updated the question and it uses python3.5m and the error still arises. The numpy it picks is also of python3.5m. I switched to cpython api completely and still it is not working. Any comments would be very helpful. Looking forward for the answer. Thanks. – Pawan Nov 22 '19 at 07:14
  • To be honest, I'm still betting that you are mixing two pythons (3.5m and 3.5), as you're picking up includes and libraries from a mix of both `/usr` and `/usr/local`. What does this `python -c "import distutils.sysconfig as sc; print(sc.get_config_var('INCLDIRSTOMAKE'))"` show? (Ie. do the directories match?) – Wim Lavrijsen Nov 23 '19 at 00:02
  • Thanks for helping out. I found out that there was some linking issue. Solution in [this Link](https://stackoverflow.com/questions/49784583/numpy-import-fails-on-multiarray-extension-library-when-called-from-embedded-pyt) worked for me. – Pawan Nov 25 '19 at 10:23