Is it possible to compile MATLAB programs that contain Python code using MATLAB Compiler?
70 views (last 30 days)
MathWorks Support Team on 12 Mar 2018
I tried compiling my MATLAB program with Python code into a standalone application, but got the following error message when running it:
Undefined variable "py" or function "py.command"
MathWorks Support Team on 18 Aug 2021
Edited: MathWorks Support Team on 6 Sep 2021
Yes, this workflow is supported.
But please pay attention to what mentioned in the link below before compiling your code:
A possible cause for this error is that the Python module is on the Python search path.
More Answers (3)
Sarah Crimi on 28 Jan 2019
I had this problem as well. I had to manually add the Python files to the folder of the executible.
Giancarlo Meccariello on 23 Mar 2021
Could you please specify how to add the file to the code?
I am using such a py. function in my standalone app..
rho=py.CoolProp.CoolProp.PropsSI('D','T',273.15+ T_gc_out ,'P',p_h,KM)
however, I don't know how to add this in my standalone programm such that the end user does not require MATLAB and can run the standalone programm without having any troubles.
Richard Barrett-Jolley on 19 Nov 2021
My experience, just end user, is that compilation of standalone MATLAB apps with python calls to specific files/modules only works if (a) those python modules/scripts are set up to run standalone themselves and the python scripts are either in the root folder of the app, or presumably accessible in the PATH.
So essentially, if I prepare a fully functional app on my PC/Mac which includes calls to custom python scripts, it really wont run on someone else's computer unless they first do all the same Python installation I did and then sort out locating the python scripts themselves.
hopefully a future release could address this. My tests were with MATLAB 2021b.