So in python native extension, is it possible to implement multiple modules in a single shared library? Then what would be the name of the shared library should be?
PyMODINIT_FUNC PyInit_foo(void) { PyModule_Create(...); }
PyMODINIT_FUNC PyInit_bar(void) { PyModule_Create(...); }
Should I name the library file foo.so
or bar.so
? and will import foo;
import just foo
or both foo
and bar
modules?
Yes you can. The easiest way is to create hardlinks named after each module for importlib
to be able to find the module. The statement import <x>
imports the module named <x>
by looking for an exported PyInit_<x>
function from <x>-*.so
.
So say, you build as foo.so
and create bar.so
as a hardlink to foo.so
. Your package structure would look like:
/mypackage/__init__.py
/mypackage/foo.so
/mypackage/bar.so -> foo.so # hardlink