"Unable to resolve name" error when calling python from MATLAB Web App Server
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 10 Aug. 2023
Beantwortet: MathWorks Support Team
am 10 Aug. 2023
I am referring to this MATLAB Answer Can I call a Python module from a web app running on Web App Server? to call a python module py.test.myfunc from my web app. I have tried both solution 1 and 2 and neither of them works. I have made sure the web app server account can access the .py file and has execution access to the file.
My python file test.py content is below
import numpy
def myfunc():
return 'hello world'
Akzeptierte Antwort
MathWorks Support Team
am 10 Aug. 2023
It turns out that it is because web app server account (MwWebAppsWorkerR202xx) doesn't have access the numpy installation. So basically it errors at "import numpy". Due to issues with our software, it didn't report errors about import functions. The error "Unable to resolve name py.test.myfunc" is misleading.
The solution is
1.Adding import module folder (numpy folder in this example) to PYTHONPATH or sys.path of Python.
AND
2.Enable at least read and execute access of MwWebAppsWorkerR202xx account to the numpy folder.
Assuming you are on Linux system and numpy folder is installed under /home/<username>/.local/lib/python3.8/site-packages/numpy/.
1. For example, if you are adding the module folder to PYTHONPATH, in your mlapp startup function, you can add below code.
function startupFcn(app, a)
setenv("PYTHONPATH","/home/<username>/.local/lib/python3.8/site-packages");
val=getenv("PYTHONPATH");
disp(['PYTHONPATH is ' val]);
end
2. To give proper access to MwWebAppsWorkerR202xx account, open up a Terminal, you can use chmod command and add sudo at the beginning if permission is denied.
For example,
chmod -R 777 /home/<username>/.local/lib/python3.8/site-packages/numpy/
Above command gives read, write and execute access to all the users.
You can use "ls -l" command to verify the proper access has been granted.
ls -l '/home/<username>/.local/lib/python3.8/site-packages/numpy'
And you should see rwxrwxrwx for all files.
Note: For Linux system, you need to have execute permission to all parent folders to get read,write and execute permission to a child folder. If you find chmod failed to grant you required access, you can check the access to all parent folders. For example,
ls -l '/home/<username>/.local/lib/python3.8/site-packages/'
ls -l '/home/<username>/.local/lib/python3.8/'
ls -l '/home/<username>/.local/lib/'
all the way to
ls -l '/home/'
You need to have at least rwx-----x access to all these folders.
Another way to verify MwWebAppsWorkerR202xx has gained access to numpy is to log in as MwWebAppsWorkerR202xx and run numpy using this account.
Below command log in as MwWebAppsWorkerR2022a, set up PYTHONPATH and run python session and import numpy. Numpy is imported successfully. It verifies you have granted access to MwWebAppsWorkerR2022a account and you should be able to use numpy successfully in your web apps.
% sudo su MwWebAppsWorkerR2022a
$ whoami
MwWebAppsWorkerR2022a
$ export PYTHONPATH='/home/<usrname>/.local/lib/python3.8/site-packages'
$ echo $PYTHONPATH
/home/<username>/.local/lib/python3.8/site-packages
$ python
Python 3.8.14 (default, Sep 8 2022, 00:02:10)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!