Run Python script with numpy and pandas in Matlab

74 visualizaciones (últimos 30 días)
Francesc Naya
Francesc Naya el 10 de Dic. de 2021
Comentada: Luc VIGNAUD el 5 de Dic. de 2022
I am trying tu run a python script using the Matlab "pyrunfile" function (Matlab version is 2021b and Python version is 3.9 using on Anaconda)
This function works when I run the following simple Python script :
> greetings = "hello"
> print(greetings)
But it does not work for another Python script that includes :
> import numpy as np
> import pandas as pd
> import matplotlib.pyplot as plt
It gives me the following error (I have checked NumPy version is 1.20.3):
Error using __init__><module> (line 48)
Python Error: ImportError:
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\R2021b\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 __init__><module> (line 145)
Error in <string>><module> (line 1)
Thank you
  1 comentario
Ahmed Ben Hassine
Ahmed Ben Hassine el 9 de Sept. de 2022
Hello ,
Could you find a solution to fix this ?
I have the same problem .
Thanks!

Iniciar sesión para comentar.

Respuestas (2)

Sean de Wolski
Sean de Wolski el 10 de Dic. de 2021
The python that MATLAB is calling apparently does not have numpy installed properly. Check your system path for the version of python in use.
A minimal working example I just made works fine:
npx.py
import numpy as np
x = np.array(1)
And
>> x = pyrunfile('npx.py','x')
x =
Python ndarray:
1
Use details function to view the properties of the Python object.
Use int64 function to convert to a MATLAB array.
>> double(x)
ans =
1
>>

Francesca Lorenzutti
Francesca Lorenzutti el 19 de Oct. de 2022
Hello,
I just had the exact same problem. Just launch matlab from the anaconda prompt. Then, you don't need to run pyversion or pyenv, just use
p=pyrunfile("myscript.py");
This solved the problem for me.
Good luck!
  1 comentario
Luc VIGNAUD
Luc VIGNAUD el 5 de Dic. de 2022
"Just launch matlab from the anaconda prompt." was the solution for me (Python 3.9 & NumPy 1.20.3)
but remains unsupported solution !!!

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by