Python GUI doesn't close after closing MATLAB

조회 수: 8 (최근 30일)
Rajiv Kumar
Rajiv Kumar 2025년 3월 2일
댓글: dpb 2025년 3월 28일
Hello Team
I am opening my python pyside6 GUI from MATLAB 2023b using pyrunfile command.
I have observed that if I close my MATLAB first, my python GUI still remains open for a very long time and I have to go to the task manager to end the task. Why is it so?
How does MATLAB handle the closing of applications?
  댓글 수: 1
dpb
dpb 2025년 3월 2일
편집: dpb 2025년 3월 2일
I suspect that is not MATLAB but the OS who is responsible when you run a Python script. The Python app is going to be running in another shell, not the MATLAB one...I think your script would need to close when done.

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

답변 (1개)

aditi bagora
aditi bagora 2025년 3월 4일
Hi Rajiv,
As @dpb pointed out when you use the pyrunfile command in MATLAB to execute a Python script, it runs the script in "Out of Process" mode. This means that MATLAB launches the Python interpreter as a separate process, which is independent of the MATLAB session. As a result, closing MATLAB does not automatically terminate the Python process. This behavior can be confirmed by running the pyenv command in MATLAB, which will show you the current execution mode.
In order to fix the issue, the best possible solution is to handle closing of the application in the script itself. However, You can change the execution mode to "In Process" using the pyenv command, but this might lead to compatibility issues with certain third-party Python libraries.
Please refer to the following MathWorks documentation links for detailed information on pyrunfile and pyenv.
  댓글 수: 2
Rajiv Kumar
Rajiv Kumar 2025년 3월 28일
Hi @aditi bagora @dpb our script is not running in "out of process" mode its in "in process" mode only.
I want the gui to be closed as soon as I close my matlab. Also I do not want to use of any System commands to close the application.
Can you provide a fix so we dont have to use sys command and able to cater the issue.
dpb
dpb 2025년 3월 28일
Have you terminated pyenv and then restarted MATLAB in "InProcess" mode after? I note the state is maintained between sessions of MATLAB so if you used something else in between it could have been reset.
Use the taskmanger to monitor, but it's still possible the python environment launches its own thread/task for the gui. In the end, it's going to be the OS and internal behavior of Python that is controlling here, not MATLAB and "it is what it is"; there is no "fix" in MATLAB.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by