MATLAB Answers

Can't import numpy or other Python libraries

조회 수: 23(최근 30일)
Brian Barry
Brian Barry 2 Oct 2020
댓글: Brian Barry 8 Oct 2020
I am trying to use a matlab wrapper for a Python library that requires numpy, along with some other dependencies. I do not have admistrator permissions on this computer (so cannot use pip or other protocols to install numpy) and have to resort to installing these libraries on user space rather than system space. I have started this process by downloading the numpy library from github. Next, I added the path where I downloaded numpy. Then I used py.importlib.import_module() with arguments 'numpy' and even the full location of numpy and both times received an error:
Error using <frozen importlib>_find_and_load_unlocked (line 953)
Python Error: ModuleNotFoundError: No module named '/home/brian/Packages/numpy'
Error in <frozen importlib>_find_and_load (line 971)
Error in <frozen importlib>_gcd_import (line 994)
Error in __init__>import_module (line 126)
return _bootstrap._gcd_import(name[level:], package, level)
I'm not sure I'm doing the right thing here, and was wondering what I'm doing wrong or if there are alternatives that will solve my issue.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(1개)

Kojiro Saito
Kojiro Saito 2 Oct 2020
You need to add your packages directory in your python's search path.
Here is an example.
pysys = py.sys.path;
pysys.append('/home/brian/Packages')
np = py.importlib.import_module('numpy');
np.array(10)
Or, instead of using py.sys.path.append, you can use PYTHONPATH enviroment.

  댓글 수: 5

표시 이전 댓글 수: 2
Brian Barry
Brian Barry 6 Oct 2020
I tried this too, no idea why it's not working...
Kojiro Saito
Kojiro Saito 7 Oct 2020
Actually, what I tried on my Windows machine was,
Download numpy package from GitHub.
Install the package in custom folder (C:\Temp\Packages) using setup.py on Command Prompt.
python setup.py install --install-lib C:\Temp\Packages
Use numpy from MATLAB Command Window.
pysys = py.sys.path;
pysys.append('C:\Temp\Packages\numpy-1.20.0.dev0+unknown-py3.7-win-amd64.egg')
np = py.importlib.import_module('numpy');
np.array(10)
I hope this helps.
Brian Barry
Brian Barry 8 Oct 2020
Thanks – I think this should fix it, but at the moment I am now having issues with user permissions for installing the cython dependency. I will update once that is resolved.

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by