title: Embedding Python in MATLAB
body: <p>I am trying to embed Python 2.6 into MATLAB (7.12).  I wanted to embed with a mex file written in C.  This worked fine for small simple examples using scalars.  However, if Numpy (1.6.1) is imported in anyway MATLAB crashes.  I say anyway because I have tried a number of ways to load the numpy libraries including</p>
  
  <ol>
  <li><p>In the python module (.py):</p>
  
  <pre><code>from numpy import *
  </code></pre></li>
  <li><p>With <code>PyRun_SimpleString</code> in the mex file:</p>
  
  <pre><code>PyRun_SimpleString(“from numpy import *”);
  </code></pre></li>
  <li><p>Calling numpy functions with <code>Py_oBject_CallObject</code>:</p>
  
  <pre><code>pOut  = PyObject_CallObject(pFunc, pArgs); 
  </code></pre></li>
  </ol>
  
  <p>Originally, I thought this may be a problem with embedding Numpy in C.  However, Numpy works fine when embedded in simple C files that are compiled from the command line with /MD (multithread) switch with the Visual Studios 2005 C compiler.   Next, I thought I will just change the make file in MATLAB to include the /MD switch.  No such luck, mexopts.bat compiles with the /MD switch.  I also manually commented out lines in the Numpy init module to find what was crashing MATLAB.  It seems that loading any file with the extension pyd crashes MATLAB.  The first of such files loaded in NumPy is multiarray.pyd.   The MATLAB documentation describes how to debug mex files with visual studios which I did and placed the error message below.  At this point I know the problem is a memory problem with the pyd’s and some conflict with MATLAB.  Interestingly, I can use a system command in MATLAB to kick off a process in python that uses numpy and no error is generated.  I will paste below the error message from MATLAB followed by the DEBUG output in visual studios of the processes that crash MATLAB.  However, I am not pasting the whole thing because the list of first-chance exceptions is very long.   Are there any suggestions for solving this integration problem?</p>
  
  <pre><code>MATLAB error
  Matlab has encountered an internal problem and needs to close 
  MATLAB crash file:C:\Users\pml355\AppData\Local\Temp\matlab_crash_dump.3484-1:
  
  
  ------------------------------------------------------------------------
         Segmentation violation detected at Tue Oct 18 12:19:03 2011
  ------------------------------------------------------------------------
  
  Configuration:
    Crash Decoding  : Disabled
    Default Encoding: windows-1252
    MATLAB License  : 163857
    MATLAB Root     : C:\Program Files\MATLAB\R2011a
    MATLAB Version  : 7.12.0.635 (R2011a)
    Operating System: Microsoft Windows 7
    Processor ID    : x86 Family 6 Model 7 Stepping 10, GenuineIntel
    Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
    Window System   : Version 6.1 (Build 7600)
  
  Fault Count: 1
  
  Abnormal termination:
  Segmentation violation
  
  Register State (from fault):
    EAX = 00000001  EBX = 69c38c20
    ECX = 00000001  EDX = 24ae1da8
    ESP = 0088af0c  EBP = 0088af44
    ESI = 69c38c20  EDI = 24ae1da0
  
    EIP = 69b93d31  EFL = 00010202
  
     CS = 0000001b   DS = 00000023   SS = 00000023
     ES = 00000023   FS = 0000003b   GS = 00000000
  
  
  Stack Trace (from fault):
  [  0] 0x69b93d31 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00081201 ( ???+000000 )
  [  1] 0x69bfead4 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00518868 ( ???+000000 )
  [  2] 0x69c08039 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00557113 ( ???+000000 )
  [  3] 0x08692b09                           C:/Python26/python26.dll+00076553 ( PyEval_EvalFrameEx+007833 )
  [  4] 0x08690adf                           C:/Python26/python26.dll+00068319 ( PyEval_EvalCodeEx+002255 )
  
  
  
  This error was detected while a MEX-file was running. If the MEX-file
  is not an official MathWorks function, please examine its source code
  for errors. Please consult the External Interfaces Guide for information
  on debugging MEX-files.
  
  If this problem is reproducible, please submit a Service Request via:
      http://www.mathworks.com/support/contact_us/
  
  A technical support engineer might contact you with further information.
  
  Thank you for your help. 
  </code></pre>
  
  <p><strong>Output from Visual Studios DEBUGGER</strong> </p>
  
  <pre><code>First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
  First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
  First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
  First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e210..
  First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e400..
  First-chance exception at 0x69b93d31 in MATLAB.exe: 0xC0000005: Access violation writing location 0x00000001.
  &gt; throw_segv_longjmp_seh_filter()
  throw_segv_longjmp_seh_filter(): invoking THROW_SEGV_LONGJMP SEH filter
  &gt; mnUnhandledWindowsExceptionFilter()
  MATLAB.exe has triggered a breakpoint
  </code></pre>