栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用python的setuptools / setup.py编译和安装C可执行文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python的setuptools / setup.py编译和安装C可执行文件?

最后通过修改

setup.py
为执行安装的命令添加了其他处理程序来解决。

setup.py
这样做的一个示例可能是:

import osfrom setuptools import setupfrom setuptools.command.install import installimport subprocessdef get_virtualenv_path():    """Used to work out path to install compiled binaries to."""    if hasattr(sys, 'real_prefix'):        return sys.prefix    if hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix:        return sys.prefix    if 'conda' in sys.prefix:        return sys.prefix    return Nonedef compile_and_install_software():    """Used the subprocess module to compile/install the C software."""    src_path = './some_c_package/'    # compile the software    cmd = "./configure CFLAGS='-03 -w -fPIC'"    venv = get_virtualenv_path()    if venv:        cmd += ' --prefix=' + os.path.abspath(venv)    subprocess.check_call(cmd, cwd=src_path, shell=True)    # install the software (into the virtualenv bin dir if present)    subprocess.check_call('make install', cwd=src_path, shell=True)class CustomInstall(install):    """Custom handler for the 'install' command."""    def run(self):        compile_and_install_software()        super().run()setup(name='foo',      # ...other settings skipped...      cmdclass={'install': CustomInstall})

现在,当

python setup.py install
被调用时,将使用自定义
CustomInstall
类,然后在正常安装步骤运行之前编译并安装软件。

您也可以对感兴趣的任何其他步骤执行类似操作(例如build / develop / bdist_egg等)。

另一种选择是使

compile_and_install_software()
函数成为的子类
setuptools.Command
,并为其创建完整的setuptools命令。

这比较复杂,但是可以让您执行一些操作,例如将其指定为另一个命令的子命令(例如避免执行两次),并在命令行上将自定义选项传递给它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号