- MATLAB Compiler: https://www.mathworks.com/help/compiler/index.html
- MATLAB Engine API for Python: https://www.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html
Calling a python command from a deployed python library
34 ビュー (過去 30 日間)
古いコメントを表示
I'm having trouble calling Python functions in a function that I've deployed to a Python library.
My MATLAB function is:
function out = test_python()
out = py.sys.path;
end
This works in MATLAB, but if I deploy and call my function from the interpreter, I get the following:
>>> import test_python
>>> runtime = test_python.initialize()
>>> runtime.test_python()
[stack trace]
MatlabRuntimeError: An error occurred when evaluating the result from a function. Details:
File 'C:\...\test_python.m, line 2, in test_python'
Undefined variable "py" or class "py.sys.path".
I know Python can be found, since
function [version, executable, isloaded] = test_python()
[version, executable, isloaded] = pyversion;
end
returns
>>> import test_python
>>> runtime = test_python.initialize()
>>> runtime.test_python(nargout=3)
('3.7', 'D:\\Users\\...\\Anaconda3\\python.EXE', False)
0 件のコメント
回答 (1 件)
sanidhyak
2025 年 6 月 25 日 21:04
編集済み: sanidhyak
2025 年 6 月 25 日 21:04
I understand that you are trying to call Python functions inside a MATLAB function and deploy it as a Python library. Here, the command "py.sys.path" works correctly in MATLAB, but when used in a compiled/deployed context, it results in an error.
This issue occurs because the "py.*" interface is not supported in compiled MATLAB applications. The MATLAB Compiler does not package or support the Python interoperability features ("py.*" syntax), which are only available within the interactive MATLAB environment.
To work around this limitation, you can refer the following:
Workaround 1: Call Python Script via System Command -
You can execute an external Python script using MATLAB's "system()" function, which works even in deployed mode:
function output = call_python_script()
[status, cmdout] = system('python myscript.py');
if status == 0
output = cmdout;
else
error('Python script failed to run: %s', cmdout);
end
end
Here, you need to make sure that Python is installed and accessible from the system’s environment path on the deployment machine.
Workaround 2: Drive MATLAB from Python (using MATLAB Engine for Python) -
Run Python as the host environment and call MATLAB from it using the MATLAB Engine API for Python. This requires installing the engine API ("matlab.engine") in Python.
Note that you cannot use the "py.*" syntax like "py.sys.path" or "py.importlib.import_module()" in deployed functions because the Python interface layer is disabled in the compiled environment.
For further reference, kindly refer to the following official documentations:
Cheers & Happy Coding!
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!