Calling python from matlab isnt working

조회 수: 14 (최근 30일)
ron schwarz
ron schwarz 2019년 9월 27일
답변: Shrinidhi KR 2020년 5월 8일
Hello,
I am trying to run a python script from matlab and for every run i do i recieve this problem:
Unable to resolve the name py.test.our_function.
The python script is this:
import numpy
def our_function(text):
print('%s %f' % (text, numpy.nan))
I tried to run pyenv, and the correct path is shown.
Thanks for your help, Ron

답변 (2개)

Dinesh Yadav
Dinesh Yadav 2019년 9월 30일
You can use MATLAB’s system function which will execute your python code as you would do on command prompt in Windows.
system('python pythonfilename.py')
system('python pythonfilename.py argument')
If you are passing a single argument.
Also check that your folder containing your python file is added to MATLAB path.

Shrinidhi KR
Shrinidhi KR 2020년 5월 8일
I suppose that your python script has the filename as test.py, which you are calling in matlab as py.test.our_function('xyz'). So the filename is causing the issue here, it is overshadowed by the other in-built module inside python installed directory. You can verify this as follows:
>> py.importlib.import_module('test')
ans =
Python module with no properties.
<module 'test' from 'C:\\Users\\XXXX\\AppData\\Local\\Programs\\Python\\Python36\\lib\\test\\__init__.py'>
So you can change the file name of your python script to something else like mytest.py, which works
>> py.importlib.import_module('mytest')
ans =
Python module with properties:
our_function: [1×1 py.function]
numpy: [1×1 py.module]
<module 'mytest' from 'H:\\Documents\\ML Answers\\mytest.py'>
>> py.mytest.our_function('abc')
abc nan

카테고리

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