Unable to load custom python package

5 views (last 30 days)
Phillipp
Phillipp on 22 Jun 2021
Commented: Ron on 28 Feb 2024
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.

Accepted Answer

Phillipp
Phillipp on 22 Jun 2021
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 Comment
Ron
Ron on 28 Feb 2024
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.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!