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

3 views (last 30 days)
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.

Answers (1)

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

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!