Issues calling python from Matlab compiled executable in a docker container on WSL2

30 次查看(过去 30 天)
I am trying to call a python script from Matlab (2022b update 3) function compiled into a docker container on WSL2. I have tried a couple methods, but no luck. For example, I tried using the following build procedure below (where the python3 executable and the python file are in ./AdditionalFiles). Calling the python fails:
Python commands require a supported version of CPython.
Error in testCompPy
MATLAB:Python:PythonUnavailable
I tried including the pyenv("Version","/python3") in the matlab script and that errors as well.
MATLAB:Pyversion:InvalidPath
even though including exist("/python3") in the compiled function shows that is there.
By adding a disp(pwd) to the Matlab code I was able to see that pwd is "/", which is why I copy the additional files there. Futhermore, apt-get commands don't seem to work but I think that might be something to do with running in a docker container inside wsl.
res = compiler.build.standaloneApplication('testCompPy.m','TreatInputsAsNumeric', false);
opts = compiler.package.DockerOptions(res,'ImageName','testcompy-standalone-app','AdditionalInstructions',...
["COPY ./AdditionalFiles/ /"]);
compiler.package.docker(res, 'Options', opts);
The resulting DockerFile looks like
FROM matlabruntime/r2022b/release/update3/300000000000000000
COPY ./applicationFilesForMATLABCompiler /usr/bin/mlrtapp
RUN chmod -R a+rX /usr/bin/mlrtapp/*
# AdditionalInstructions
COPY ./AdditionalFiles/ /
RUN useradd -ms /bin/bash appuser
USER appuser
ENTRYPOINT ["/usr/bin/mlrtapp/testCompPy"]
Any advice or ideas would be appreciated. Also, any insight into what capabilities from Ubuntu are in the matlabruntime docker image would also be helpful.
For reference, the m-file being compiled is
function testCompPy(varargin)
disp(pwd)
disp(pyenv)
disp(exist("/python3"))
% Tried with and without the following:
pyenv("Version",'/python3')
in = {'Jones','Johnson','James'};
N = py.list(in);
names = py.mymod.search(N);
disp(string(names))
  1 个评论
Steven Bordonaro
Steven Bordonaro 2023-2-9
I was able to get this to work with the following;
res = compiler.build.standaloneApplication('testCompPy.m',...
'TreatInputsAsNumeric', false);
opts = compiler.package.DockerOptions(res,'ImageName','testcompy-standalone-app', ...
'AdditionalInstructions',...
["RUN apt-get update && \" ...
"apt-get install -y python3 python3-dev && \" ...
"apt-get clean", ...
"COPY ./AdditionalFiles/ /"]);
compiler.package.docker(res, 'Options', opts);

请先登录,再进行评论。

回答(1 个)

Dinesh
Dinesh 2023-3-29
Hi Steven!
It is great to know that you have solved the issue yourself. Matlab docker images are based on Debian, a Linux distribution.
Docker allocates a read-write filesystem to the container. Suppose your host system is Windows and the container image uses Linux, binaries that work on Windows (.exe files) will not be compatible inside your container and that is most likely the reason for the error.
opts = compiler.package.DockerOptions(res,'ImageName','testcompy-standalone-app', ...
'AdditionalInstructions',...
["RUN apt-get update && \" ...
"apt-get install -y python3 python3-dev && \" ...
"apt-get clean", ...
"COPY ./AdditionalFiles/ /"]);
In the Docker options, you specified installing python3 in the container. This installs python3 and python3-dev for Linux, which is why you can make it work. Installing all dependencies that your container needs directly rather than copying them is always better.

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by