Python GUI doesn't close after closing MATLAB
8 views (last 30 days)
Show older comments
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
Answers (1)
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
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.
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!