Python dependency via direct calls to py in class method?
1 次查看(过去 30 天)
显示 更早的评论
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...
0 个评论
采纳的回答
Martin Caron
2022-11-7
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!