Matlab Python JIT Bug
The Matlab external language interface to Python in Matlab releases before R2022a has a bug with the JIT compilation via
Matlab execution engine
such that any mention of the py.*
namespace will make a function unusable if Python is not
available.
Especially problematic is functions that can work without Python (say in an if-else or switch block) where the mere presence of a py.*
call will cause the entire function to fail if Python is not available.
To workaround this issue in Matlab older than R2022a, use a private function to encapsulate the Python call. This way, the main function will not be affected by the Python call.
function result = my_fun2(input)
if has_python()
result = encaps_fun2(input);
else
% fallback code when Python is not available
end
end
Make a private function in file “private/encaps_fun2.m”:
function result = encaps_fun2(input)
result = py.some_module.some_function(input);
end