MATLAB Answers


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

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"


No tags entered yet.


2 Answers

Answer by MathWorks Support Team on 13 Aug 2019
Edited by MathWorks Support Team on 13 Aug 2019
 Accepted Answer

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.


Glad to hear this workflow is supported, can you explain how to do it?
My installed executable doesn't contain my python code, so the script cannot find the Python module I'm calling from my compiled Matlab script.
When I manually add the python file to the "application" directory my executable has been installed to, it works just fine.
My mcc commands include `-a C:\path\to\` but I had to select that file manually as the deploytool did not find it.
Just to clarify I:
  • have python installed
  • have the 64bit version of python
  • matlab CAN find python (I can print py.sys.path, for example)
  • there isn't an error in the module (I can run it from regular Matlab just fine)
  • there are no module name conflicts
It sure does seem to me like a bug w/ Matlab, but I'd love to be shown otherwise!
Adding the file to "Files installed for your end user" did the trick.

Sign in to comment.

Answer by 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.


Sign in to comment.