编辑及;使用python安装C可执行文件';设置工具/setup.py?
我有一个python模块,它调用从C源代码构建的外部二进制文件 该外部可执行文件的源代码是python模块的一部分,作为.tar.gz文件分发 是否有一种方法可以解压缩、编译该外部可执行文件并使用setuptools/setup.py安装它 我想要实现的是:编辑及;使用python安装C可执行文件';设置工具/setup.py?,python,python-3.x,setuptools,setup.py,Python,Python 3.x,Setuptools,Setup.py,我有一个python模块,它调用从C源代码构建的外部二进制文件 该外部可执行文件的源代码是python模块的一部分,作为.tar.gz文件分发 是否有一种方法可以解压缩、编译该外部可执行文件并使用setuptools/setup.py安装它 我想要实现的是: 将该二进制文件安装到虚拟环境中 使用setup.py install,setup.py build等管理二进制文件的编译/安装 将二进制代码作为python模块的一部分,这样它就可以作为一个轮子分发,而无需外部依赖 最后通过修改setup
- 将该二进制文件安装到虚拟环境中
- 使用
,setup.py install
等管理二进制文件的编译/安装setup.py build
- 将二进制代码作为python模块的一部分,这样它就可以作为一个轮子分发,而无需外部依赖
setup.py
为完成安装的命令添加额外的处理程序来解决
执行此操作的setup.py
示例可能是:
import os
from setuptools import setup
from setuptools.command.install import install
import subprocess
def 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 None
def 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
类,然后在运行正常安装步骤之前编译并安装软件
您还可以对您感兴趣的任何其他步骤执行类似操作(例如,构建/开发/bdist_等)
另一种方法是将编译和安装软件()
函数作为setuptools.Command
的子类,并为其创建一个功能完备的setuptools命令
这更为复杂,但允许您将其指定为另一个命令的子命令(例如,避免执行两次),并在命令行中将自定义选项传递给它。您解决了这个问题吗?如果是,你能分享你的解决方案吗?谢谢你最后的回答,我很快会补充一个答案。