Unable to load custom python package

4 vues (au cours des 30 derniers jours)
Phillipp
Phillipp le 22 Juin 2021
Commenté : Ron le 28 Fév 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.

Réponse acceptée

Phillipp
Phillipp le 22 Juin 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 commentaire
Ron
Ron le 28 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by