MATLAB py.sys.executable on Windows points to MATLAB.exe and not python.exe

1 次查看(过去 30 天)
I'm working on calling a Python library from MATLAB. The sys.executable property, which should refer to the Python executable, actually points to MATLAB.exe but only on Windows.
  • Python on LInux sys.executable is a Python string /usr/XXX/bin/python, as it should be
  • MATLAB in Linux: py.sys.executable is /usr/XXX/bin/python, as it should be
  • Python on Windows, sys.executable is "C:\..\python.exe", as it should be
  • MATLAB on Windows, py.sys.executable is "C:\...\MATLAB.exe" , wrong!
This was tested in Windows 10 with MATLAB R2022a and Python 3.9.13

采纳的回答

Paul Rutten
Paul Rutten 2022-8-15
Hi Michael,,
I can confirm that the py.sys.executable points to the MATLAB executable on Windows 10 with MATLAB 19a, 21a, 21b, and 22b (I do not have 22a installed). I do not know about a workaround for this.
The sys.executable attribute in the Python process also points to the MATLAB executable. This can be worked around by expanding on the workaround provided for the incorrect [sys.prefix and sys.exec_prefix] attributes in the Python process and ensuring that the sys.executable attribute in the Python process points to the correct executable.
For me overriding the incorrect attribute values in the Python sys module resolved some issues with using Python modules from MATLAB.
  1 个评论
Michael Posa
Michael Posa 2022-8-15
Thank you. The link you pointed to do did create a workaround. For completeness, from MATLAB, I can call
if ispc
py.FixSysExecutable.SetSysExecutable(pyenv().Executable)
end
using the Python file FixSysExecutable.py.
import sys
def SetSysExecutable(path):
sys.executable = path
return sys.executable

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by