Segmentaion fault while calling Python script using system()

6 views (last 30 days)
I'm using system() to call an external python script that does some file processing for me but I get a segmentation fault with exit code 139 when I try and run. EX: system('python my_python_script.py')
I am confident that it is not an issue with the python script as I can run it successfully through the terminal without Matlab. The script uses some environment variables and all of my python libraries are installed in a virtual environment (where I launch Matlab from). I'm thinking the issue is either with the virtual environment or the environment variables but I am not 100% sure what to do to ensure that the all the dependencies are shared between Matlab's sytem call and my normal terminal call to the script. I've reviewed the information on this site: https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html but I'm not sure about the environment variables. Any suggestions?
EDIT: I should have added more information. I am running on Ubuntu 16.04 and Matlab 2021a. The python script does some parallel processing of data and saves off the results that Matlab does not need access to. Really I just want to call the Python script in a loop with variable inputs as the Matlab script does some of the pre-processing before the Python script can run. I also checked that Matlab's system call has access to the environment variables that the Python script uses and it does so I believe it's an issue with one of the libraries that Matlab uses that is incompatible with what I am trying to do in Python. I don't know how to find out which library might be causing this however if that is the case.

Answers (1)

Peter O
Peter O on 7 Jul 2021
A couple thoughts:
  1. Regarding your python call: are you on a recent version of MATLAB? You can call the script directly using MALTAB's py syntax. If you type pyenv at the command line it should display what version of Python MATLAB thinks is available. It might be cleaner for MATLAB to handle spinning up and down the Python instance? It should also be a nicer interface if you are exchanging data.
  2. Environment Variables: See if MATLAB is reading them correctly? Type !set in the command window of your environment to get a listing (Windows assumed)
  3. Segfault: If MATLAB is not directly interacting with the script or its variables, the segfault makes me think the problem is when something returns from the system call. How far along does it get if you ask the program to system('python script.py', '-echo')? If you alter an environment variable along the way, MATLAB will not see the change and react...poorly.
Does any of that help? What sort of processing are we talking here? Are MATLAB and Python looking into the same memory space? Which version of Python? Anaconda or stock?
  1 Comment
Robert Hakulin
Robert Hakulin on 7 Jul 2021
Thanks for the response.
1. I tried using the py syntax as well with the same result and I am running 2021a on Ubuntu 16.04. Using the pyenv shows that it is using the venv that I want it to use
2. Matlab is finding all of the environment variables that I need so that is not the issue
3. It doesn't get very far. It is hard to tell since I'm not sure what a great way to debug a seg fault through a Matlab system call is (since it works fine running through terminal)
This was helpful though. I think I've narrowed down the possible options to a library/dependency issue between Matlab and Python. Now I need to find out what that dependency is.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!