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

조회 수: 4 (최근 30일)
Dominik Mattioli
Dominik Mattioli 2020년 4월 8일
답변: colordepth 2025년 3월 10일
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?

채택된 답변

colordepth
colordepth 2025년 3월 10일
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.

추가 답변 (0개)

카테고리

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