Embedding Python in MATLAB

Each Answer to this Q is separated by one/two green lines.

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

  1. In the python module (.py):

    from numpy import *
    
  2. With PyRun_SimpleString in the mex file:

    PyRun_SimpleString(“from numpy import *”);
    
  3. Calling numpy functions with Py_oBject_CallObject:

    pOut  = PyObject_CallObject(pFunc, pArgs); 
    

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?

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. 

Output from Visual Studios DEBUGGER

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.
> throw_segv_longjmp_seh_filter()
throw_segv_longjmp_seh_filter(): invoking THROW_SEGV_LONGJMP SEH filter
> mnUnhandledWindowsExceptionFilter()
MATLAB.exe has triggered a breakpoint

Try to approach the problem from the Python side: Python is a great glue language, I would suggest you to have Python run your Matlab and C programs. Python has:

  1. Numpy
  2. PyLab
  3. Matplotlib
  4. IPython

Thus, the combination is a good alternative for almost any existing Matlab module.

With matlab 2014b a possibility to call python functions directly in m code was added.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .