OiO.lk Blog C# Numpy array not initializing when trying to call python script from C code
C#

Numpy array not initializing when trying to call python script from C code


I am working on a project where I have to integrate neural network models with a larger project built primarily using C and C++.

I want to now integrate numpy array and according to the official documents I have to call a function called import_array() under the numpy/arrayobject.h header. I have successfully compiled and linked all the files together and everything compiles well without any errors.

But as soon as the import_array() function is called my program crashes. I am not able to find good documentation around the issue which can help me. The code which runs the program generates a backtrace file of the crash log which suggests that there is a long division error during the import_array() call. I have tried running this on MacOs (M2 chip) and Linux to no avail.

This is my C file

#include <Python.h>
#include <numpy/arrayobject.h> // Include NumPy header for array support

int call_python() {
  // Initialize the Python interpreter
  Py_Initialize();

  // Set the Python path to include the virtual environment's site-packages
  PyObject* sysPath = PySys_GetObject("path");

  // Path to your virtual environment's site-packages
  PyObject* venvPath = PyUnicode_DecodeFSDefault("path/to/venv");
  PyList_Append(sysPath, venvPath);
  Py_DECREF(venvPath);
  
  import_array();

  // Import the Python module
  PyObject* pathValue = PyUnicode_DecodeFSDefault("path/to/workspace");
  PyList_Append(sysPath, pathValue);
  Py_DECREF(pathValue);

  PyObject* pName = PyUnicode_DecodeFSDefault("my_python_module");
  PyObject* pModule = PyImport_Import(pName);
  Py_DECREF(pName);

  if (pModule != NULL) {
    // Get the function from the module
    PyObject* pFunc = PyObject_GetAttrString(pModule, "my_function");

    if (pFunc && PyCallable_Check(pFunc)) {
      // Call the Python function
      PyObject* pArgs = PyTuple_New(0); // No arguments
      PyObject* pValue = PyObject_CallObject(pFunc, pArgs);
      Py_DECREF(pArgs);

      if (pValue != NULL) {
        // Process the returned value
        printf("Result: %ld\n", PyLong_AsLong(pValue));
        Py_DECREF(pValue);
      } else {
        PyErr_Print();
      }
      Py_DECREF(pFunc);
    } else {
      PyErr_Print();
    }
    Py_DECREF(pModule);
  } else {
    PyErr_Print();
  }

  // Finalize the Python interpreter
  Py_FinalizeEx();
  return 0;
}


This is my python script (my_python_module.py)

import numpy as np

def my_function():
    arr = np.array([1, 2, 3])
    print(arr)
    return 42



You need to sign in to view this answers

Exit mobile version