Segmentaion fault while calling Python script using system()

조회 수: 8 (최근 30일)
Robert Hakulin
Robert Hakulin 2021년 7월 6일
댓글: Robert Hakulin 2021년 7월 7일
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.

답변 (1개)

Peter O
Peter O 2021년 7월 7일
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
Robert Hakulin
Robert Hakulin 2021년 7월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by