How can I properly create an standalone APP which includes python .py scripts?

47 views (last 30 days)
Hello, I am facing difficulties to properly compile an standalone Matlab APP which includes python .py scripts. It works fine inside Matlab 2022a environment, however the standalone APP (from APP Designer) does not work, it sounds a beep and the .py scripts don´t run properly. Is there a guide or educational material that I could follow to make that in the proper way?
Is there something that I should include in the Startup FCN ?
Thanks,
  2 Comments
Walter Roberson
Walter Roberson on 15 Aug 2022
Python is run as an external program; MATLAB Compiler would not include the Python executable as part of the bundle.
Fernando Sarracini Júnior
The .py files are copied inside the same folder of Matlab APP executable file. I am using pyrunfile to call the .py files like below. I have python installed in my computer (confirmed with pe = pyenv).
[res, vectorf] = pyrunfile("vscs_compilation.py", ["z", "vectorf"], file = file_final)
It works fine inside Matlab, however the standalone executable file does not work. Am I missing anything?
Thanks,

Sign in to comment.

Answers (2)

Eric Delgado
Eric Delgado on 26 Sep 2022
Look... pyenv will give you the Python environment used to call your .py files. Ok. But... when you run a standalone version of your app, you should be aware that the machine running the app could not have a Python installation (or could have one, but without all required libs). And you do have to point to the correct Python environment (inside your Matlab Runtime or Matlab+Compiler).
I had to deal with this issue... and the solution was to give the user the possibility to change the Python environment (because by default Matlab will point to the "base environment" of the Python installed). So... this is the callback for a "python button" pushed that I put in my apps.
% Button pushed function: version_python
function version_pythonPushed(app, event)
Python = pyenv;
[file, pathfile] = uigetfile({'python*.exe', 'python*.exe'}, 'Choose the Python executable file...', Python.Executable);
if file
try
Python = pyenv('Version', fullfile(pathfile, file));
catch ME
MessageBox(app, 'error', getReport(ME))
end
end
end
  2 Comments
Mani
Mani on 19 Jul 2024
Edited: Mani on 19 Jul 2024
Does your app work when it's running on the third party machine without MATLAB installed? I did the same (asking user to enter the python path), but the app is failing to fetch python executive.
Had you done any additional tweak?
Eric Delgado
Eric Delgado on 20 Jul 2024
Edited: Eric Delgado on 20 Jul 2024
I understand that you are running a standalone version of an app built in AppDesigner, and for that, the user has installed MATLAB Runtime on their machine, correct?
Additionally, the user must install Python (I always suggest Miniconda) and create the virtual environment from which they should locate the "python.exe" file, mapping it in MATLAB Runtime.
pyenv('Version', 'C:\Users\userName\.conda\envs\envName\python.exe'); % (using conda default path in Windows)
Read the answer below provided by Mathworks Support Team:

Sign in to comment.


Kevin Rusch
Kevin Rusch on 7 Mar 2024
Edited: Kevin Rusch on 7 Mar 2024
The way to accomplish what you're looking for is by using a tool like pyinstaller to convert your python files into standalone executables, leveraging MATLAB's isdeployed, ispc, ismac, and isunix functions to alter the call you make within your app, and packaging your python and MATLAB standalones using any of many third party solutions. This way your code should work regardless of your user's environment.
You still won't end up with a single standalone app in the end, but you'll have a single package that creates a directory on the user's system that will execute your code as intended.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!