How to use python code with numpy in Matlab?

조회 수: 66 (최근 30일)
Yasuyuki Hamanaka
Yasuyuki Hamanaka 2022년 3월 16일
댓글: Peter O 2022년 12월 5일
Hello, I'm trying to use python code in matlab.
As How can I use Python pandas in MATLAB? - (mathworks.com) shows, I installed anaconda and try to run my python code with pyrunfile.
clear all;
pyrunfile('test.py');
However, numpy seems to occur some errors below.
Error using __init__><module>
Python Error: ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "C:\Program Files\MATLAB\R2022a\bin\win64\MATLAB.exe"
* The NumPy version is: "1.20.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.
Error in <string>><module> (line 25)
From anaconda prompt, python is version 3.9 and numpy is version 1.20.3.
And, Matlab is connected to the anaconda
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "C:\Users\middl\anaconda3\python.EXE"
Library: "C:\Users\middl\anaconda3\python39.dll"
Home: "C:\Users\middl\anaconda3"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "17884"
ProcessName: "MATLAB"
It looks like pandas is working. Would you tell me why I cannot use numpy?

채택된 답변

Peter O
Peter O 2022년 3월 16일
To my knowledge, the Anaconda distribution remains an unsupported version of Python for MATLAB. Numpy fails with Anaconda because Anaconda maps the DLL's in a non-standard spot and adds them to the path when you start Anaconda's Prompts or Navigators. Pandas is straight Python. To be fast, numpy uses compiled C-code, hence those extra DLL's. The workaround I've used successfully in the past is to launch Matlab from an Anaconda prompt.
Again, this is an unsupported way to do things. I've noticed that matplotlib has a tendency to crash this way. Of course, MATLAB's plotting engine and tools are better :)
Steps:
  1. Launch Anaconda Prompt or Anaconda Powershell Prompt
  2. Type matlab at the prompt. Matlab will launch with extra environment variables configured by Anaconda's startup.
  3. Test in MATLAB:
py.math.sqrt(2)
py.numpy.sqrt(2)
Hope this helps!
  댓글 수: 3
Luc VIGNAUD
Luc VIGNAUD 2022년 12월 5일
Worked also for me
but " unsupported way to do things" is somehow unsupportable from Mathworks !
Peter O
Peter O 2022년 12월 5일
Yeah, my hunch would be that because the Anaconda distribution is bundled by a third party, which is neither Python Foundation nor MathWorks, it would add a lot of overhead to support and potentially interfacing with a third organization. For instance locating the source of a configuration or versioning bug just got twice as long. To be clear, you can achieve everything offered from the Anaconda distribution with stock python and pip. The Anaconda version adds ease of use for newcomers, data science specific package configuration, and built-in version/environment containerization.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by