Calling python from matlab isnt working
9 次查看(过去 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
0 个评论
回答(2 个)
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.
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!