Calling python from matlab isnt working

9 ビュー (過去 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 Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by