Calling python from matlab isnt working

13 次查看(过去 30 天)
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