- When packing the ctf file with deploytool, add the Python module to "Files required for your app to run". Or -a the Python module when packaging with mcc.
- In the app designer startup function add the following code:
Can I call a Python module from a web app running on Web App Server?
19 views (last 30 days)
Show older comments
MathWorks Support Team
on 23 Jul 2020
Edited: MathWorks Support Team
on 19 Jul 2022
I have created a web app using App Designer that calls a Python module like so:
py.myModule.myFunction()
The app appears to work when run in MATLAB, but when I deploy it to Web App Server, I get an error in the web app log stating:
Unable to resolve the name py.myModule.myFunction.
Accepted Answer
MathWorks Support Team
on 14 Jul 2022
Edited: MathWorks Support Team
on 19 Jul 2022
Yes, you can call Python modules from deployed web apps.
First make sure your web app is configured to run python by following this instruction .
About the specific error for a python module:
Unable to resolve the name py.myModule.myFunction.
It indicates that Python cannot find the module on the PYTHONPATH.
There are different solutions:
Solution 1.
You can package the Python module (.py file) into the web app server .ctf file. To do so:
if isdeployed
[filepath,~,~] = fileparts(mfilename('fullpath'));
insert(py.sys.path, int64(0), filepath);
end
Solution 2.
If you don't want to package the Python module into the web app .ctf file, you will need to add the directory containing your Python module to the PYTHONPATH system environment variable. (If it is not defined, you can simply create it.)
Note that since Web App Server runs under a different user account, you will need to define PYTHONPATH as a system environment variable, not a user environment variable, so that it applies to all users.
Furthermore, the account from which Web App Server runs must have permission to access the folder containing your Python module. For example, you can put the python module file in the same folder as where the web app server ctf file is, namely the web app server app folder (e.g., C:\ProgramData\MathWorks\webapps\R2021b\apps).
After setting up the environment variable, restart web app server.
0 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Web App Server in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!