problem with python numpy

조회 수: 305 (최근 30일)
Johann Thurn
Johann Thurn 2018년 10월 31일
댓글: Lukas Brandl-Cheng 2022년 12월 17일
I have started to try to use python functionality in matlab. The Textwrapper example works. I now tried other packages, and everytime there is some dependency on numpy I get this message:
>> py.help('numpy')
problem in numpy - ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: Das angegebene Modul wurde nicht gefunden.
If I try to use numpy directly, I get this:
>> x = py.numpy.random.random([4,4]);
Undefined variable "py" or class "py.numpy.random.random".
Calling
help('textwrap')
or
help('numpy')
in Python works fine. Calling
py.help('textwrap')
in Matlab works,
py.help('numpy')
does not.
I have tried to update numpy and to remove and reinstall it, but this does nothing. I am using Python 3.6 which should be supported.
>> pyversion
version: '3.6'
executable: 'C:\ProgramData\Anaconda3\python.exe'
library: 'C:\ProgramData\Anaconda3\python36.dll'
home: 'C:\ProgramData\Anaconda3'
isloaded: 1
Maybe I am doing something wrong?

채택된 답변

David Garrison
David Garrison 2020년 8월 27일
Beginning in MATLAB R2018b, Python functions that accept numpy arrays may also accept MATLAB arrays without explicit conversion. When necessary, a numpy array can be created explicitly from a MATLAB array. For example, if you have a supported version of Python that is installed with the numpy library, you can do the following:
>> x = rand(2,2); % MATLAB array
>> y = py.numpy.array(x); % numpy array
y =
Python ndarray:
0.5943 0.8064
0.6133 0.1372
Use details function to view the properties of the Python object.
Use double function to convert to a MATLAB array.
Also beginning in MATLAB R2018b, it is possible to convert numeric numpy arrays returned from Python into MATLAB arrays. For example:
>> y = py.numpy.random.random([int32(2), int32(2)]) % numpy array
y =
Python ndarray:
0.5943 0.8064
0.6133 0.1372
Use details function to view the properties of the Python object.
Use double function to convert to a MATLAB array.
>> x = 2*double(y) % MATLAB array
x =
1.1885 1.6129
1.2266 0.2744
See the MATLAB documentation on Passing Matrices and Multidimensional Arrays for additional Information.

추가 답변 (4개)

Prashanth Ramesh
Prashanth Ramesh 2018년 11월 5일
Please note that MATLAB does not support Anaconda Python. To call Python libraries from MATLAB, you need to install a supported version of the reference implementation (CPython) for Python. MATLAB supports versions 2.7, 3.5, and 3.6. You can download CPython from https://www.python.org/downloads/
However possible workarounds for using Anaconda Python are listed below (though not officially supported):
1. Launch MATLAB from a properly configured Anaconda prompt. Then import numpy.
2. In Anaconda installation directory, copy the files %env_root%/libraries/bin/mkl_* to %env_root%/lib/site-packages/numpy/core.
  댓글 수: 10
Daniel Vieira
Daniel Vieira 2021년 12월 21일
편집: Daniel Vieira 2021년 12월 21일
"2. In Anaconda installation directory, copy the files %env_root%/libraries/bin/mkl_* to %env_root%/lib/site-packages/numpy/core."
worked for me as well, also Python 3.8
Lukas Brandl-Cheng
Lukas Brandl-Cheng 2022년 12월 17일
2. worked for me too. As Paul Kuberry mentioned, the mkl files were in %env_root%/Library/bin/mkl_*. Additionally, I had to copy them to %env_root%/Lib/site-packages/numpy/core rather than %env_root%/lib/site-packages/numpy/core
Thanks for the solution!

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


Igor Varfolomeev
Igor Varfolomeev 2020년 4월 9일
편집: Igor Varfolomeev 2020년 4월 9일
I've noticed that simply adding the folder with the DLLs, related to numpy to PATH, just before starting Python also fixes the issue (R2019b update 5, Windows 10 version 1803).
For example, I create the environment with
conda create -n test_py36 python=3.6 numpy
and use setenv like this:
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','OutOfProcess'... --- this (new mode) causes misc. issues
'ExecutionMode','InProcess'... --- this (old mode) works OK
);
py.importlib.import_module('numpy')
(inspired by this)
  댓글 수: 3
Binxu
Binxu 2020년 10월 8일
This works like a charm for me!!! No need to copy paste directories!
Flavio
Flavio 2022년 4월 17일
It worked very well.

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


Vaibhav Arora
Vaibhav Arora 2019년 4월 6일
Install Python (Cpython) from python.org/downloads.
Make sure the version you download is 64-bit if your Matlab exe is 64-bit. By default, a 32-bit cersion of python is downloaded so be careful.
Open python and try the command "import numpy as np". If you get an error, you need to install the library. For this, go to command prompt and type (for python 3): "pip3 install numpy"
Now you will be able to use numpy library in matlab
  댓글 수: 1
Curious
Curious 2022년 7월 15일
It isn't working. I have matlab R2018a and python 3.10. I have to use numpy in a matlab program. Kindly help

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


I am not a robot
I am not a robot 2019년 9월 9일
If you have setup an env(-ironment) in Anaconda, you need to setup the pyversion command to use the env.
Adapt the below to suit your needs
pyversion('C:\Users\Your_User_Name\AppData\Local\Continuum\anaconda3\envs\Your_Env\pythonw.exe')
  댓글 수: 3
Paul Kuberry
Paul Kuberry 2020년 3월 26일
After doing the above command, you would be left with the same problem as the OP.
The issue is mkl_ libraries not being where they can be found through MATLAB. Prashanth Ramesh's solution resolves this.
Seth Wagenman
Seth Wagenman 2020년 8월 31일
This works generally, Bram ter Huurne:
P.S. Where is the "general matlab-python website" where you suggest posting solutions, "as Anaconda is the general python distribution software for most users?"

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

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by