Calling Python function making use of numpy results in strange behavior

I have a tiny Python function which simply prints the installed numpy version. I call this function from Matlab. I have Python 3.7.4 (Anaconda) and Matlab R2020a.
Python code (python_module.py) looks like this.
import numpy as np
def print_numpy_version():
print('Numpy version is {}'.format(np.__version__))
return
If I call the print_numpy_version function from Matlab as shown below then everything works fine. I see the numpy version printed in Matlab command window. (My Python virtual environment is named opencv-env.)
close all;clear;clc;
setenv('PATH',[getenv('PATH'),'C:\Anaconda3\envs\opencv-env\Library\bin;']);
py.python_module.print_numpy_version();
However, if I try to call print_numpy_version from Matlab as shown below then I get an error.
close all;clear;clc;
try_this();
function try_this()
setenv('PATH',[getenv('PATH'),'C:\Anaconda3\envs\opencv-env\Library\bin;']);
py.python_module.print_numpy_version();
end
Error message is:
Unable to resolve the name py.python_module.print_numpy_version.
Error in main>try_this (line 10)
py.python_module.print_numpy_version();
Error in main (line 5)
try_this();
This is very strange. The only difference between the working Matlab code and the failing one is that in the latter I am calling Python function from within a Matlab function. Furthermore, this failure only happens with numpy. If I try to print the version of another Python package (say opencv) then Matlab code works fine in both cases.
Any idea what's causing this starnge behavior?

回答 (0 件)

カテゴリ

製品

リリース

R2020a

質問済み:

2020 年 9 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by