Can I call a Python module from a web app running on Web App Server?

19 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 14 de Jul. de 2022
Editada: MathWorks Support Team el 19 de Jul. de 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:
  1. 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.
  2. In the app designer startup function add the following code:
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.

Más respuestas (0)

Categorías

Más información sobre MATLAB Web App Server en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by