I know how to make C code from Python using cythonize -o mymodule.c mymodule.py
or using the cython
command. The resulting C file has to be compiled to an Python extension using gcc or any other C compiler, and I'm also able doing that. If I want it really simple, I just use distutils
/setuptools
and python setup.py build_ext
.
But what I can't figure out is how to make an extension file (pyd/so) with a DIFFERENT name than the source file. I can't simply rename the file due to the fact that there has to be a function PyInit_modulename
in the extension, and I can't change modulename.pyd
to _modulename.pyd
, that would require PyInit__modulename
and throw this exeption:
ImportError: dynamic module does not define init function (PyInit__modulename)
Basically it should be like this:
mymodule.py -> _mymodule.pyd
This avoids e.g. the import confusion if I have two files with the same name but a different ending (py/pyd/pyc/pyo/so/dll):
mydir/
mymodule.py
mymodule.pyd
Code:
from mymodule import func
func()
# Is this now from the extension or from the python code?
# And how to access tho other one?
What I need is:
mydir/
mymodule.py
_mymodule.pyd <- Note the underscore!
Code:
import mymodule as script
script.func()
# Run the code from the python file
import _mymodule as extension
extension.func()
# Run code from the extension
But how on earth can I compile like this?
Thank you in advance!
Just one thought I had: I think I need to start somewhere at the C code generation with cython.