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

4 次查看(过去 30 天)
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.

回答(1 个)

surya venu
surya venu 2024-7-19,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.

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by