Python dependency via direct calls to py in class method?

1 visualización (últimos 30 días)
Hello,
When trying instantiate a class, I'm getting an error related to CPython:
Error using SomeDummyClass
Python commands require a supported version of CPython. See Getting Started with Python.
None of the object's properties are Python objects. What's the deal? My class has an unrelated function that calls python directly using py. Could that be the issue? I currently don't have python installed on the workstation in question. I don't need it - I don't need the function that calls python for what I am working on.
Thanks!
P.S. I really hope a guy named Martin Caron can answer this question...

Respuesta aceptada

Martin Caron
Martin Caron el 7 de Nov. de 2022
Hi --Person who I've never met-- Steven,
I happen to have a few insights on the matter. Doing a quick example class (see below), the following observations were made when instanciating this class. Note that we'll only cover the instanciation case, not the calling of those functions, as it is pretty easy to see that they will crash.
classdef SomeDummyClass
properties
Foo (1,1)double
end
methods
function SomethingThatClearlyUsesPython(this)
% This crashes when python is not installed
py.SomePackage.SomeFunction();
end
function SomethingThatIndirectlyUsesPython(this)
% This does not crash
ThisFunctionCallsPyDotSomePackageDotSomeFunction();
end
function out = SomethingThatUsesANonexistantClass(this, other)
arguments
this
other (1,1)ThisClassDoesNotExist
end
% This does not crash
out = ThisClassDoesNotExist();
end
end
end
When python is not installed, only direct calls to "py." cause the error on instanciation. If we comment out the whole "SomethingThatClearlyUsesPython" function, the instanciation works without any issue.
From this, I would hypothesize that:
Matlab does not care about the contents of class methods during instanciation, except if there is "py." somewhere inside it.
Would be nice if a Matlab person could confirm if this statement is truly accurate, but so far it seems to be empyrically correct.
I would recommend moving the direct calls to "py." outside of your class, this will allow you to load the data without caring about what is contained inside the functions.
Cheers!
  1 comentario
Steven Giannacopoulos
Steven Giannacopoulos el 8 de Nov. de 2022
Wow! Thanks for your help! Your answer is so good, I feel like I participated in the process. I owe you a coffee at the machine in the Connect Zone. Cheers!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by