Is it possible to compile MATLAB programs that contain Python code using MATLAB Compiler?

70 views (last 30 days)
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"

Accepted Answer

MathWorks Support Team
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.

Sign in to comment.

More Answers (3)

Sarah Crimi
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.
  1 Comment
Abdelrahman Helaly
Abdelrahman Helaly on 29 Nov 2020
does matlab by default package the required python libraries?
or it just runs the python code on the machine using the default python interpreter on that machine

Sign in to comment.

Giancarlo Meccariello
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
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.


No tags entered yet.


Community Treasure Hunt

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

Start Hunting!

Translated by