I maintain a Python package and I want to precompile code with Numba.
The precompiled code should live in a submodule of that package in order to not pollute the global namespace.
For reference, the package mytestpackage
could look like this:
from setuptools import setup, find_packages
from numba.pycc import CC
cc = CC("shouldbesubmodule")
@cc.export("foo", "void()")
def foo():
print("Hello, World!")
setup(
ext_modules=[cc.distutils_extension()],
name="mytestpackage",
version="1.0.0",
packages=find_packages(),
zip_safe=False)
And I want to be able to import the submodule like import mytestpackage.shouldbesubmodule
python3 setup.py install
cd /tmp
# this works:
python3 -c "from shouldbesubmodule import foo; foo()"
# but I want this to work instead:
python3 -c "from mytestpackage.shouldbesubmodule import foo; foo()"
I thought that maybe this could be as easy as specifying the package in the call to CC
similar to cffi:
cc = CC("mytestpackage.shouldbesubmodule")
but that does not work because dots are explicitly disallowed in that string.
Changing the Numba source code here to ext = _CCExtension(name="mytestpackage.shouldbesubmodule",
works, but I would like to be able to do that without modifying the Numba source code.
The CC-Object has a parameter source_module
, but if it is intended for this use case somehow, it is not clear to me how to use it.
In conclusion: How can I precompile a submodule shouldbesubmodule
for a package mytestpackage
so that import mytestpackage.shouldbesubmodule
works?