MATLAB Answers

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

Asked by MathWorks Support Team

### MathWorks Support Team (view profile)

on 12 Mar 2018
Latest activity Edited by MathWorks Support Team

### MathWorks Support Team (view profile)

on 13 Aug 2019
Accepted Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

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"

### Tags

No tags entered yet.

## 2 Answers

### MathWorks Support Team (view profile)

Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

on 13 Aug 2019
Edited by MathWorks Support Team

### MathWorks Support Team (view profile)

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.

Matthew Parrilla

### Matthew Parrilla (view profile)

on 17 Jul 2018
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\file.py 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!
Matthew Parrilla

### Matthew Parrilla (view profile)

on 19 Jul 2018
Adding the file to "Files installed for your end user" did the trick.

Sign in to comment.

### Sarah Crimi (view profile)

Answer by Sarah Crimi

### Sarah Crimi (view profile)

on 28 Jan 2019

I had this problem as well. I had to manually add the Python files to the folder of the executible.

#### 0 Comments

Sign in to comment.