Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change output filename in setup.py (distutils.extension)

Here's my setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

wrapper = Extension(
    name="libwrapper",
    ...
)
setup(
    name="libwrapper",
    ext_modules=cythonize([wrapper])
)

When I run python3 setup.py build_ext the output file is called libwrapper.cpython-36m-x86_64-linux-gnu.so, but I want to name it only libwrapper.so, how can I do that?

like image 261
user10207893 Avatar asked Oct 22 '25 05:10

user10207893


2 Answers

Try the following code. sysconfig.get_config_var('EXT_SUFFIX') returns platform-specific suffix which can be removed from the final filename by subclassing build_ext and overriding get_ext_filename.

from distutils import sysconfig
from Cython.Distutils import build_ext
from distutils.core import setup
import os

class NoSuffixBuilder(build_ext):
    def get_ext_filename(self, ext_name):
        filename = super().get_ext_filename(ext_name)
        suffix = sysconfig.get_config_var('EXT_SUFFIX')
        ext = os.path.splitext(filename)[1]
        return filename.replace(suffix, "") + ext


setup(
    ....
    cmdclass={"build_ext": NoSuffixBuilder},
)

Final filename will be test.so

like image 130
hurlenko Avatar answered Oct 23 '25 21:10

hurlenko


A slightly more minimal version of hurlenko's answer with the option of including whatever suffix you want, in case anyone else wanted this

import setuptools
from distutils.command.build_ext import build_ext
import os

class MySuffix(build_ext):
    suffix = '.library'
    def get_ext_filename(self, ext_name):
        return os.path.join(*ext_name.split('.')) + self.suffix

setuptools.setup(
    ....
    cmdclass={'build_ext': MySuffix},
)
like image 30
Howard Coffin Avatar answered Oct 23 '25 20:10

Howard Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!