Borrar filtros
Borrar filtros

Python scripts with numpy don't work when called from MATLAB

4 visualizaciones (últimos 30 días)
AS
AS el 23 de Jun. de 2018
Respondida: surya venu el 19 de Jul. de 2024 a las 4:52
Hi,
I'm trying to call a python script I've written, from within my MATLAB code. I'm having a problem where I can't call any scripts that import numpy.
For example, the following file (abc.py) works without any issues:
def foo():
return 55
but if I add the import statement, it ceases to work.
import numpy as np
def foo():
return 55
The MATLAB error I get is
Undefined variable "py" or class "py.abc.foo".
Does anyone have any idea on how to fix this? I'm running the latest version of Anaconda for my python.

Respuestas (1)

surya venu
surya venu el 19 de Jul. de 2024 a las 4:52
Hi,
The issue you're encountering is likely due to MATLAB not being able to locate the correct Python environment where "numpy" is installed. Here are some steps you can follow to resolve this issue:
1) Verify Python Environment in MATLAB: Ensure that MATLAB is using the correct Python environment. You can do this by setting the Python environment in MATLAB using the "pyenv" function.
pyenv('Version', 'path_to_your_python_executable');
2) Check if "numpy" is Installed: Ensure that "numpy" is installed in the Python environment that MATLAB is using. If numpy is not installed, you can install it using:
conda install numpy
3) Restart MATLAB: After setting the Python environment and ensuring "numpy" is installed, restart MATLAB to make sure the changes take effect.
By following these steps, MATLAB should be able to locate and use the correct Python environment with numpy installed.
Hope it helps.

Categorías

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

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by