【问题标题】:Change output filename in setup.py (distutils.extension)在 setup.py (distutils.extension) 中更改输出文件名
【发布时间】:2020-02-18 15:32:43
【问题描述】:

这是我的 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])
)

当我运行python3 setup.py build_ext 时,输出文件名为libwrapper.cpython-36m-x86_64-linux-gnu.so,但我只想将其命名为libwrapper.so,我该怎么做?

【问题讨论】:

    标签: python python-3.x setup.py distutils distutils2


    【解决方案1】:

    试试下面的代码。 sysconfig.get_config_var('EXT_SUFFIX') 返回特定于平台的后缀,可以通过继承 build_ext 并覆盖 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},
    )
    

    最终文件名将是test.so

    【讨论】:

    • 你能用你的建议扩展我的setup.py 文件吗?目前还不清楚该怎么做。
    • 只需将NoSuffixBuilder 添加到您的setup.py 文件和cmdclass=... 到您的setup 函数并使用python setup.py build_ext 构建扩展程序
    • 回溯(最近一次调用最后一次):文件“code/cython/setup.py”,第 9 行,在 类 NoSuffixBuilder(build_ext):NameError: name 'build_ext' is not defined
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2012-09-18
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多