Cython: Question: Is it possible to avoiding cpython-36m-x86_64-linux-gnu from generated module?

Created on 16 Jun 2017  Â·  3Comments  Â·  Source: cython/cython

When I use terminal commands, I can produce the desired module with the name of my choice, that is to match the name of my .pyx and .c files.

When I use the setup.py though, I get a module with cpython-36m-x86_64-linux-gnu appended as a suffix.

Is there a way to tell setup.py to generate a shared object with the same name as my generated .c file?

Most helpful comment

For who is also curious about the rationale of the suffix like cpython-36m-x86_64-linux-gnu, please see PEP3149.

It's actually something determined by Python instead of Cython.
Don't worry about it, as Python would search for the following file names when extension module foo is imported (in this order):

foo.cpython-XYm.so
foo.abi3.so
foo.so

All 3 comments

Finally, after an intensive investigation for a while now around this peculiarity, I have managed to produce the desired results!

Special thanks to https://stackoverflow.com/a/40193040 for sharing this custom code.

Here's a full working setup.py sample code:

import os
import sysconfig

from distutils.core import setup
from Cython.Build import cythonize
from Cython.Distutils import build_ext


def get_ext_filename_without_platform_suffix(filename):
    name, ext = os.path.splitext(filename)
    ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')

    if ext_suffix == ext:
        return filename

    ext_suffix = ext_suffix.replace(ext, '')
    idx = name.find(ext_suffix)

    if idx == -1:
        return filename
    else:
        return name[:idx] + ext


class BuildExtWithoutPlatformSuffix(build_ext):
    def get_ext_filename(self, ext_name):
        filename = super().get_ext_filename(ext_name)
        return get_ext_filename_without_platform_suffix(filename)


setup(
    name="foo",
    cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
    ext_modules=cythonize("foo.pyx")
)

Enjoy people!

For who is also curious about the rationale of the suffix like cpython-36m-x86_64-linux-gnu, please see PEP3149.

It's actually something determined by Python instead of Cython.
Don't worry about it, as Python would search for the following file names when extension module foo is imported (in this order):

foo.cpython-XYm.so
foo.abi3.so
foo.so

And, please, do not remove the extension. It's a feature!

Was this page helpful?
0 / 5 - 0 ratings