我需要在Python中运行Matlab脚本.为了实现这一点,首先我安装了Matlab的调试器(MCR),使用了this个文档.然后,我使用this文档创建并安装了matlab脚本的python模块.特别是,我使用了Matlab的SDK应用程序来创建模块,然后使用python setup.py install
安装创建的目录,setup.py
在生成的目录中,我可以使用以下代码在Python内部本地执行脚本:
import test_function
mypkg = test_function.initialize()
arg1 = 1
arg2 = 2
out1, out2 = mypkg.test_function(arg1, arg2, nargout=2)
print(out1, out2)
mypkg.terminate()
现在我想从Docker容器内部执行模块.因此,我安装了MCR安装zip文件(Linux 64从here),解压缩并保存到目录matlab_runtime
.在Dockerfile中,我用Matlab SDK模块复制了目录(test_function
),用MCR安装目录(matlab_runtime
)复制了目录,并try 安装Matlab.以下是对Dockerfile的更改:
FROM mathworks/matlab-runtime-deps:R2023b
# other not-related commands
# ...
# other not-related commands
RUN apt-get install -y python3.9 python3-pip
COPY test_function /app/test_function
COPY matlab_runtime /app/matlab_runtime
RUN ls /app/matlab_runtime
RUN pip install setuptools
WORKDIR /app
USER root
RUN chmod +x ./matlab_runtime/install
RUN python3 test_function/setup.py install
RUN sudo -H ./matlab_runtime/install
但是我得到了这个错误:failed to solve: rpc error: code = Unknown desc = process "/bin/sh -c sudo -H ./matlab_runtime/install" did not complete successfully: exit code: 42
.
我try 在Dockerfile中使用make
:
RUN apt update && apt install -y make
RUN make ./matlab_runtime/install
Docker不会返回任何错误,但是当我在Docker容器中运行上面的Python脚本时,我得到了错误:AttributeError: module 'LC_filter_for_AI' has no attribute 'initialize'
,当我使用dir(test_function)判断模块的方法时,我得到了:
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
而对于模块的本地安装,
['_PathInitializer', '__builtins__', '__cached__', '__doc__', '__exit_packages', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_pir', 'atexit', 'glob', 'importlib', 'initialize', 'initialize_runtime', 'os', 'pdb', 'platform', 're', 'sys', 'terminate_runtime', 'weakref']