title: How do I access a numpy array in embedded python from c++?
body: <p>What would be a good way to access a 2dim numpy array in c++? I've already checked out the numpy/c api and some other posts but that doesn't brought me further. Here's the situation:</p>
  
  <p>I defined in a python file called Testfile.py the following numpy array:</p>
  
  <pre><code>import numpy as np
  A = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
  </code></pre>
  
  <p>Now, I would like to access this array in c++ to use it for further calculations. Here is what i did so far. 
  Note: For simplicity, i left out error handling and reference counting code snippets.</p>
  
  <pre><code>#define NPY_NO_DEPRECATED_APINPY_1_7_API_VERSION
  #define PY_ARRAY_UNIQUE_SYMBOL cool_ARRAY_API
  #include &lt;Python.h&gt;
  #include &lt;arrayobject.h&gt; // numpy!
  #include &lt;iostream&gt;
  using namespace std;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  
  int main(){
  
  // Name of input-file
  char pyfilename[] = "Testfile";
  
  // initilaize python interpreter
  Py_Initialize();
  import_array(); 
  
  // load input-file
  PyObject *pyName = PyUnicode_FromString(pyfilename);
  PyObject *pyModule = PyImport_Import(pyName);
  
  // import my numpy array object
  char pyarrayname[] = "A";
  PyObject *obj = PyObject_GetAttrString(pyModule, pyarrayname);
  
  //------------------------------------------------------------
  // The Problem starts here..
  
  // Array Dimensions
  npy_intp Dims[] = { PyArray_NDIM(obj) }; // array dimension
  Dims[0] = PyArray_DIM(obj, 0); // number of rows
  Dims[1] = PyArray_DIM(obj, 1); // number of columns
  
  // PyArray_SimpleNew allocates the memory needed for the array.
  PyObject *ArgsArray = PyArray_SimpleNew(2, Dims, NPY_DOUBLE);
  
  // The pointer to the array data is accessed using PyArray_DATA()
  double *p = (double *)PyArray_DATA(ArgsArray);
  
  for (int i = 0; i&lt;Dims[0]; i++)
  {
      for (int j = 0; j&lt;Dims[1]; j++)
          {
               p[i * Dims[1] + j] = *((int *)PyArray_GETPTR2(obj, i, j));
          }
  }
  // -----------------------------------------------------------
  
  
  Py_Finalize();
  
  return 0;
  }
  </code></pre>
  
  <p>I use python 3.6 and MSVC 2015.</p>
  
  <p><strong>EDIT:</strong> I added the headers i use and changed the problem formulation a bit.</p>
  
  <p><strong>EDIT:</strong> I added the proposed solution strategies provided by <strong>Swift</strong> and <strong>Alan Stokes</strong></p>