Unable to load custom python package

조회 수: 5 (최근 30일)
Phillipp
Phillipp 2021년 6월 22일
댓글: Ron 2024년 2월 28일
Hey guys.
I have a problem when trying to use a python script in a matlab file. I've followed the instructions in the DOC "Call User-Defined Python Module" and the tutorial works.
But I have a custom package that I need to use. But I don't even get the file to run when I add the package import. My Python file looks like this:
from package import * %Import custom package from installed Conda environment
def solve(var):
do stuff
%Calculation using the functions defined in cstmpkg
return var %Return value to matlab for further processing
Is there anything that I did wrong? I installed python with conda and changed pyversion to the corresponding conda environment where the package was installed. Or is Matlab unable to use custom Packages? That would answer the question, but it would be sad, because that would make the feature kinda useless.
Thanks for the Ideas/answers.

채택된 답변

Phillipp
Phillipp 2021년 6월 22일
I found the solution myself.
you have to start matlab directly from conda while the correct environment ist activated. (type "matlab" in conda).
And add this to you matlab script:
pathofDocument = fileparts(which('custompythonscript.py'));
if count(py.sys.path,pathofDocument) == 0
insert(py.sys.path,int32(0),pathofDocument);
end
I hope that this will help someone out there.
  댓글 수: 1
Ron
Ron 2024년 2월 28일
Hello. I am having an exactly same problem where I have downloaded a python library from Github. I have also installed python on my system. Now I have no idea about how to use this python library in matlab. I dont know anything about conda or other things. Can you please help me?? Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by