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

조회 수: 3 (최근 30일)
AS
AS 2018년 6월 23일
답변: surya venu 2024년 7월 19일
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일
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에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by