Standalone mcc app cannot import class within python module

16 ビュー (過去 30 日間)
Jiaqi Li
Jiaqi Li 2023 年 2 月 8 日
編集済み: Rushikesh 2024 年 9 月 27 日
I have an app made via mcc. In this app I need to call a python 2.7 class within a python 2.7 module. I am able to insert the path to the module to the python search path via
insert(py.sys.path, int32(0), PythonModulePath)
I am also able to import the module via
py.importlib.import_module('module')
whose return lists all the correct classes and modules.
but when I try to initialize the class py.module.class('init args') The complied class returns:
Unable to resolve the name py.module.class
MATLAB:undefinedVarOrClass
What is going on here? Why is it unable to identify the class?

回答 (2 件)

Divyank
Divyank 2023 年 2 月 8 日
Hello @Jiaqi Li, it seems that the issue is with the syntax for accessing the class within the imported module.
Try accessing the class using the following code and see if that resolves the issue:
module = py.importlib.import_module('module')
MyClass = module.class
obj = MyClass('init args')

Rushikesh
Rushikesh 2024 年 9 月 27 日
編集済み: Rushikesh 2024 年 9 月 27 日
MATLAB R2021b does not support calling python class names and initializing it using dot operator. Instead you need to use "py.setattr" to pass data to class constructor and "py.getattr" to access function object from class.
Seems like repeated question, here is reference to my detailed answer:

カテゴリ

Help Center および File ExchangePython Package Integration についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by