Effacer les filtres
Effacer les filtres

Calling python from matlab isnt working

13 vues (au cours des 30 derniers jours)
ron schwarz
ron schwarz le 27 Sep 2019
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

Réponses (2)

Dinesh Yadav
Dinesh Yadav le 30 Sep 2019
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 le 8 Mai 2020
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

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by