Python dependency via direct calls to py in class method?

3 views (last 30 days)
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...

Accepted Answer

Martin Caron
Martin Caron on 7 Nov 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 Comment
Steven Giannacopoulos
Steven Giannacopoulos on 8 Nov 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!

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!