How to run a python script with an open terminal window on a mac?

17 views (last 30 days)
I'm using a socket connection which requires the python script to continuously run. I have this figured out on Windows, where
!python main.py &
launches the python script that initialized the socket with MATLAB and listens for transmissions. I'm really struggling with this on mac, because the same call does not open a terminal (or seem to do anything).
Suggestions?

Accepted Answer

colordepth
colordepth on 10 Mar 2025 at 9:32
On a Windows-based system, doing a
!<shell-command> &
will launch a terminal window to execute the shell command, which is what you are observing. However, on a Unix-based system, doing so will immediately return control to the MATLAB command window and execute "<shell-command>" in the background. You can rest assured that your python script has launched and is running as a background process until either your MATLAB session exits or your python script finishes execution. Here's an old related discussion: https://www.mathworks.com/matlabcentral/answers/85938-how-to-open-new-terminal-visible-from-matlab-linux-mac.
Some limitations with this approach is that one is unable to see the output of the python script and one cannot terminate the exact python process as the Process ID (PID) is not known.
It may be beneficial for you to utilize MATLAB's External Interface for Python and run your script in "Out of Process" execution mode as documented here: https://www.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-python-functionality.html. This gives the ability to terminate the python process manually using the "terminate" command: https://www.mathworks.com/help/matlab/ref/pythonenvironment.terminate.html.

More Answers (0)

Categories

Find more on Python Package Integration 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!