Unable to resolve the name py.module

I am trying to call a custom python module in Matlab and I have no issues with running the python standard library modules with py.whatever but when I go to run py.mymodule, I get the error "Unable to resolve the name py.mymodule"
I am able to import the module using py.importlib.import_module('mymodule'), but I cannot call the module or the functions within it.
Any ideas?

8 commentaires

Kojiro Saito
Kojiro Saito le 25 Oct 2019
Your mymodule contains classes with methods, or just methods without a class?
Michael Rego
Michael Rego le 25 Oct 2019
Kojiro,
My module is a simple .py script that has functions that make API calls. I'm not doing anything with objects or classes.
jesus ceron
jesus ceron le 21 Mar 2020
I am facing the same problem. Did you find a solution?
David Redding
David Redding le 7 Avr 2020
I also...
Shrinidhi KR
Shrinidhi KR le 8 Mai 2020
Modifié(e) : Shrinidhi KR le 8 Mai 2020
Can you share your 'mymodule' script with just briefly showing the functions? Also what is the output that you get for py.importlib.import_module('mymodule') in matlab command window
Ulugbek Djuraev
Ulugbek Djuraev le 13 Juin 2020
i am getting this error
'Python Error: AttributeError: type object 'h5py.h5.H5PYConfig' has no attribute '__reduce_cython__''
Can youguys help me figure out what this is?
Thank you
Tom
Tom le 2 Juil 2020
I have the same issue, did you solve it?
Jordi Macià
Jordi Macià le 16 Juil 2020
I also find the same problem. Some times it loads, and others I get this message:
import py.ifcopenshell
Error: The import statement 'import py.ifcopenshell' cannot be found or cannot be imported. Imported names must end with '.*' or be fully qualified.

Connectez-vous pour commenter.

Réponses (4)

Dan Hazen
Dan Hazen le 27 Nov 2019

2 votes

Just because I was caught up with a similar issue that was due to my inexperience:
I had a Python file named testme with a function (def testme) inside of it that printed hello world.
To run in MATLAB, I had to do py.testme.testme() to get it to execute, otherwise I was getting the error you described above. The issue seems to be with referencing the module and then the function within.

1 commentaire

This suggestion seems to work for me. Thank you!
>> py.scipy.odr()
Unable to resolve the name py.scipy.odr.
>> py.scipy.odr.odr()
Python Error: TypeError: odr() missing required argument 'fcn' (pos 1)

Connectez-vous pour commenter.

Shishir Singhal
Shishir Singhal le 28 Juil 2020

1 vote

Hi,
Everytime when you make changes in a python funtion. You need to reload it again into the MATLAB environments using "py.reload("<python_module>")".
As a workaround, please follow the steps given in the documentation here: https://in.mathworks.com/help/matlab/matlab_external/call-modified-python-module.html
Hope it helps!!!
Thanks.

1 commentaire

Ahmed Al-Saffar
Ahmed Al-Saffar le 5 Août 2020
No it doesn't. The question above never mentioned doing changes. I wish I can delete your answer.

Connectez-vous pour commenter.

Daniel Toker
Daniel Toker le 31 Oct 2019

0 votes

I am running into this same problem, but only sometimes - sometimes py.mymodule works, and other times the exact same code turns up this error. Any progress with this?

1 commentaire

Akshay Dhyani
Akshay Dhyani le 24 Nov 2020
I am having a same issue. Sometime it works. some time it doesn't

Connectez-vous pour commenter.

Chen
Chen le 14 Mai 2021
Modifié(e) : Chen le 14 Mai 2021
I am new to Python as well, but I found these solutions after spending some time of debugging. There are two possible reasons for "Unable to resolve the name py.mymodule"
Reason 1:
your python script is not found by the interpreter.
Solution: copy your script into a folder
<your correct Python version folder>\Python38\Lib\site-packages\<mymodule>
Create an empty dummy file __init__.py
Can someone tell me if there's another way to do it?
Reason 2:
Matlab is NOT built to run a python module, like how you would run it in a Python IDE:
run mymodule
You should create a function inside mymodule.py
example,
inside mymodule.py:
....
def mymodfunc ():
... <python codes>
Then run this in matlab:
py.mymodule.mymodfunc(<some arguments>)
According to instructions, whenever you modify any python scripts, remember to reload them, as per instructions in topic:
Reload Modified User-Defined Python Module
However, this doesn't work directly as per claimed, you have to wrap the reload module within a function. Apparently, this is a known bug that still hasn't been fix for some time:
function reloadPy()
warning('off','MATLAB:ClassInstanceExists')
clear classes
mod = py.importlib.import_module('mymodule');
py.importlib.reload(mod);
end

Catégories

Produits

Tags

Modifié(e) :

le 14 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by