How to add Python Libraries which are referenced in a Python routine

조회 수: 1 (최근 30일)
Tom Sutherland
Tom Sutherland 2021년 2월 4일
댓글: Sean de Wolski 2021년 2월 11일
I want to call a python routine from Matlab and run it and get the resultant data back from the routine to process in Matlab. The python routine "myfunc.py" imports a bunch of libraries, numpy, time and others. When I go through the Matlab explaination of how to call a python function I get an error saying it can't find numpy.
Error using mymod><module> (line 3)
Python Error: ModuleNotFoundError: No module named 'numpy'
Error in <frozen importlib>_call_with_frames_removed (line 219)
Error in <frozen importlib>exec_module (line 728)
Error in <frozen importlib>_exec (line 630)
Error in __init__>reload (line 169)
I have looked at the references in Matlab "https://www.mathworks.com/help/matlab/matlab_external/differences-between-matlab-python.html" and others but they seem to just show how to call a function defined inside a library. Can you call a function like myfunc.py , shown below, and use it even if it imports libraries ?
Looks like I can run a function "add2" in the myfunc1.py file by using the following
val = py.myfunc1.add2(2,3) ---> Will work
BUT
val = py.myfunc2.add2(4,5) ---> will give me errors that it can't find the libraries numpy and/or time
DO I HAVE TO LOOK INTO EVERY PYTHON FILE AND SOMEHOW ADD THOSE LIBRARIES ONE AT A TIME SO MATLAB SEES THEM?
------ Python Code ------------------------
myfunc1.py
def add2 (a,b):
c = a+b
return c
myfunc2.py
import numpy as np
import time
def add2(a,b):
numsum = np.add(a,b)
return numsum

답변 (3개)

Sean de Wolski
Sean de Wolski 2021년 2월 4일
Are you sure the python environment (specified by pyenv) is numpy aware?

Tom Sutherland
Tom Sutherland 2021년 2월 4일
I am running Spyder(Python 3.8) and the python files work there. How do I tell where Spyder is getting numpy from and how to tell Matlab to get it from the same place?

Tom Sutherland
Tom Sutherland 2021년 2월 4일
My Matlab
PythonEnvironment with properties:
Version: "3.7"
Executable: "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe"
Library: "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python37.dll"
Home: "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "16392"
ProcessName: "MATLAB"
But I have installed Anaconda3 and using Spyder and its packages are in
C:\ProgramData\Anaconda3\Lib\site-packages
How do I get Matlab to "see" them or use them?
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2021년 2월 11일
Specify the executable or version in the input to pyenv(). It should be pointing at Python 3.8 not Python 3.7.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by