Can't import numpy or other Python libraries

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.

Answers (1)

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.

8 Comments

I think I'm on the right track with this suggestion, but now it isn't recognizing numpy's methods. When I call
np = py.importlib.import_module('numpy')
the command line returns
Python module with no porperties.
<module 'numpy' (namespace)>
and when I call a method like np.array I get
Unrecognized method, proerty, or field 'array' for class 'py.module'
It's because you're pointing to one level higher directory.
If you have __init__.py and setup.py under /home/brian/Packages/numpy/numpy, could you try the following?
pysys.append('/home/brian/Packages/numpy')
I tried this too, no idea why it's not working...
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.
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.
@Kojiro Saito. Thank you so much for providing the solution but can you please explain the part
"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"
In your code there is no mention of numpy and no mention of directory where you have kept it. I am new to matlab and dont know much about how to import custom python libraries in matalb. Please help me.
The post was in 2020. In those days, setup.py was included in numpy. You can find it in the other branches, for example, https://github.com/numpy/numpy/tree/maintenance/1.26.x.
You can change the package directory with pip install..
pip install --target=C:\Temp\Packages numpy
Sir thankyou so much for your kind attention. I want to use acoular, a python library, in matlab but i dont know how to do it. I have downloaded the file from Github Acoular, I have installed python 3.10 for my matlab 2023a. Can you help me for the same?? thankyou so much. The command " pip " will it work in matlab too? Sorry but i have no idea about python.

Sign in to comment.

Products

Release

R2019a

Asked:

on 2 Oct 2020

Commented:

Ron
on 28 Feb 2024

Community Treasure Hunt

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

Start Hunting!