How to run a python script with an open terminal window on a mac?
19 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Python Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!