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

16 ビュー (過去 30 日間)
Dominik Mattioli
Dominik Mattioli 2020 年 4 月 8 日
回答済み: colordepth 2025 年 3 月 10 日 9:32
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 日 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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePython Package Integration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by