Matlab Python Interface Missing sys attributes

조회 수: 6 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2021년 10월 7일
편집: MathWorks Support Team 2021년 10월 12일
The Python API in MATLAB does not have the correct values for some attributes of the sys module. As a consequence, some built_in modules do not work when called in MATLAB. For instance, disutils.sysconfig.get_python_lib(standard_lib = True) should return the full path to the standard library of Python modules. Instead, it returns './Lib'. The reason for this is that sys.prefix and sys.base_prefix, which should contain information about the full path to the Python executable that is used to instantiate pyenv, contain null strings in MATLAB.
With Python versions 3.x, this can be seen quite easily by running the following two lines in MATLAB and seeing that both are null python strings.
>> py.sys.prefix>> py.sys.base_prefix
How can I get MATLAB to populate these attributes with the correct values?
 

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 10월 7일
편집: MathWorks Support Team 2021년 10월 12일
This is a known limitation of the MATLAB Interface for Python Interface for Python versions 3.x and MATLAB versions R2021b and earlier. If downgrading to Python 2.7 is not a viable option, then there is a workaround that can be used to avoid this limitation.
1) Create the following Python file (called SysGetSys.py here).
import sys
def setPrefix(val):
    sys.prefix=val
    sys.exec_prefix=sys.prefix
    print(sys.prefix)
def getPrefix():
    print(sys.prefix)
2) Execute the following commands in MATLAB
>> s = py.list;   % this loads Python
>> env = pyenv;
>> py.SetGetSys.setPrefix(env.Home)   %this sets the new sys.prefix
>> py.SetGetSys.getPrefix % confirm the sys.prefix is set
After that, sys.prefix should work as expected.
 

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by