Python GUI doesn't close after closing MATLAB

8 views (last 30 days)
Rajiv Kumar
Rajiv Kumar on 2 Mar 2025
Commented: dpb on 28 Mar 2025
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 Comment
dpb
dpb on 2 Mar 2025
Edited: dpb on 2 Mar 2025
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.

Sign in to comment.

Answers (1)

aditi bagora
aditi bagora on 4 Mar 2025
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 Comments
Rajiv Kumar
Rajiv Kumar on 28 Mar 2025
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 on 28 Mar 2025
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!