Unable to load custom python package

5 次查看(过去 30 天)
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