MATLAB Answers

Why can py.numpy.array not be resolved?

70 views (last 30 days)
Hello,
I am trying run python from within Matlab. Unfortunately, I I even fail to use numpy (see below). I would very much appreciate any suggestions as to why that could be. I am using Matlab 2019b on Winows 10.
>> pyversion
version: '3.7'
executable: 'C:\Users\wolf5212\AppData\Local\Programs\Python\Python37\python.exe'
library: 'C:\Users\wolf5212\AppData\Local\Programs\Python\Python37\python37.dll'
home: 'C:\Users\wolf5212\AppData\Local\Programs\Python\Python37'
isloaded: 0
>> py.str(1)
ans =
Python str with no properties.
1.0
>> py.numpy.array([])
Unable to resolve the name py.numpy.array.
Importing numpy from the command prompt is no problem.
H:\>python
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 01:54:44) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> exit()

Accepted Answer

Igor Varfolomeev
Igor Varfolomeev on 9 Apr 2020
I ran into a similar issue recently, but I'm not sure it it's 100% same.
What I can suggest is:
2. Try "InProcess" mode:
pe=pyenv('Version','C:\path\to\Python36\pythonw.exe',...
...'ExecutionMode','OutOfProcess'... --- this (new mode) causes misc. issues in R2019b update 5
'ExecutionMode','InProcess'... --- this (old mode) works OK
);
3. Try to explicitly import numpy:
pe=pyenv('Version','C:\path\to\Python36\pythonw.exe',...
'ExecutionMode','InProcess');
py.importlib.import_module('numpy')
if it produces something like this:
>> py.importlib.import_module('numpy')
Error using __init__><module> (line 54)
Python Error: ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
<...>
it is likely that Python cannot see the necessary dll files. Adding them to path fixes the issue for me.
This only happens to me when I use Anaconda python installation :
conda create -n test_py36 python=3.6 numpy
I never ran into this issue when using CPython 3.6.8.
But for Anaconda, the issue could be fixed by simply adding the folder with the neccessary dlls to %PATH%:
username = getenv('username');
setenv('PYTHONUNBUFFERED','1'); % doesn't change much, but just-in-case
setenv('path',['C:\Users\' username '\Anaconda3\envs\test_py36\Library\bin;', getenv('path')]);
pe=pyenv('Version',['C:\Users\' username '\Anaconda3\envs\test_py36\pythonw.exe'],...
'ExecutionMode','InProcess');
py.importlib.import_module('numpy')
  2 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!