Matlab fails to run Python built with Pybind
Show older comments
Attempting to call a member of a user-defined Python module which was built using Pybind. The module seems to import correctly via
mymodule = py.importlib.import_module("mymodule")
But on attempting to access a class from the module, I get this error:
>> myclass = mymodule.myclass
Cannot find class 'py.pybind11_builtins.pybind11_type'.
I've tested that the module loads and works correctly when accessed in Python. I'm using Matlab on Ubuntu, and Python 3.10
6 Comments
Infinite_king
on 16 Oct 2023
Edited: Infinite_king
on 16 Oct 2023
Are you able to import and use functions in python modules from MATLAB ?
Jay Castro
on 18 Oct 2023
Grace Kepler
on 23 Oct 2023
This issue could be related to using custom C++ datatypes with pybind11 and currently may be a limitation for Python Interface.
Sylwester Arabas
on 18 Nov 2023
For the record, this has also been reported on pybind11 issue tracker back in May 2022: https://github.com/pybind/pybind11/issues/3945 (where pybind11 maintainers suggested to report it to Matlab maintainers).
It's a big showstopper as there are more and more Python packages built using pybind11.
Sylwester Arabas
on 22 Nov 2023
Update: a workaround has just been proposed and succesfully tested here: https://github.com/pybind/cmake_example/pull/164
David Truscott
on 11 Dec 2023
This is the work-around for that error that worked for me on Windows:
Create a file called 'pybind11_builtins.py' and place it in the folder where your python modules library resides. (For my Python 3.10 installation in Windows, it was {Path to Python Installation} \Lib folder.
The only line in that file should be 'pybind11_type=type' (without the apostrophes).
Reload your pyenv in Matlab,
>>py.importlib.import_module('pybind11_builtins')
>>py.importlib.import_module(' Your originally intended module here')
Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!