problem with python numpy
古いコメントを表示
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?
採用された回答
その他の回答 (4 件)
Prashanth Ramesh
2018 年 11 月 5 日
16 投票
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 件のコメント
Johann Thurn
2018 年 11 月 10 日
Nicholas Richmond
2019 年 5 月 21 日
- Launching the Anaconda Prompt and activating my desired environment, then:
- Launching MATLAB from that prompt, e.g.:
(someEnvironment) C:\Users\someUser>matlab
This allows me to use modules which are not visible to the python.exe associated with someEnvironment, but are able to be imported once someEnvironment is loaded.
Bram ter Huurne
2020 年 2 月 21 日
Thanks! Was looking for ages for this post. Maybe mention on the general matlab-python website, as Anaconda is the general python distribution software for most users.
Paul Kuberry
2020 年 3 月 26 日
Nicholos already mentioned that the first suggestion worked for him. I want to post an update on the second option.
2. In Anaconda installation directory, copy the files %env_root%/libraries/bin/mkl_* to %env_root%/lib/site-packages/numpy/core.
Recent versions of Anaconda install these files into %env_root%/Library/bin/mkl_* rather than %env_root%/libraries/bin/mkl_*
This is a one-time fix, and allows you to call MATLAB in your own traditional way (from a GUI shortcut, for instance).
krishna maganti
2020 年 4 月 10 日
編集済み: krishna maganti
2020 年 4 月 10 日
Worked on my MAC. Created a virtual env and then ran matlab from the env and set path and run. Done.
Thank you very much.
Seth Wagenman
2020 年 8 月 31 日
Nicholas Richmond if I launch MATLAB from python do I get the normal GUI or just the engine/interpreter?
jkh
2021 年 1 月 14 日
A related option to 1 could be to launch Windows cmd from Matlab and start a temporary Anaconda environment there. This was for Windows with Anaconda used for Python.
Example:
I created a file, named basicPyFunc.py with
# Basic test of running Anaconda Python using matlab
import numpy as np
#%% basic definition
def just_sqrt(x_in):
x_in = np.float64(x_in) #convert to float if needed
return np.sqrt(x_in)
#%% run the main (from command line)
if __name__ == '__main__':
import sys
in_num = 144 #use 144 if no other input, sqrt(144) is 12
if len(sys.argv) >= 2: #input number provided as an input
in_num = sys.argv[1]
out_num = '%s' % just_sqrt(in_num) #output number as string
sys.stdout.write(out_num) #output the result
then in Matlab
username = getenv('USERNAME');
conda_actv_path = ['C:\Users\', username, '\AppData\Local\Continuum\anaconda3\Scripts\activate.bat']; %happens to be where my installation is...
[~,out_num]=system(['cmd /C "', conda_actv_path, ' & python basicPyFunc.py 64"']);
which gave the result of sqrt(64) = 8.0.
Worked for me without starting Matlab from the Anaconda prompt and this type of format worked for other modules I have besides numpy.
Liwei Hua
2021 年 3 月 5 日
"2. In Anaconda installation directory, copy the files %env_root%/libraries/bin/mkl_* to %env_root%/lib/site-packages/numpy/core."
This way works great for me. I have Python version 3.8.
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
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
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')
4 件のコメント
Christopher Wingard
2020 年 6 月 17 日
This worked for me. No messing around with copying directories or changing the way I launch Matlab.
Binxu
2020 年 10 月 8 日
This works like a charm for me!!! No need to copy paste directories!
Flavio
2022 年 4 月 17 日
It worked very well.
Movin
2024 年 10 月 5 日
Hi, I'm trying to use coolprops with matlab and I have the same trouble (with numpy). I'm not too familiar with creating environments with anaconda, is there another way of doing the same thing you've done but without anaconda
Vaibhav Arora
2019 年 4 月 6 日
2 投票
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
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
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 件のコメント
Bram ter Huurne
2020 年 2 月 21 日
This generally does not work.
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
2020 年 8 月 31 日
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?"
カテゴリ
ヘルプ センター および File Exchange で Call Python from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!