首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Pythonsetuptools/distutils为Makefile的“ext”包定制构建

Pythonsetuptools/distutils为Makefile的“ext”包定制构建
EN

Stack Overflow用户
提问于 2018-03-15 23:42:57
回答 1查看 0关注 0票数 0

序言:Pythonsetuptools用于包分发。我有一个Python包(让我们称之为my_package),有几个extra_require包裹。一切都可以找到(安装和构建包,以及额外的,如果被要求),因为所有。extra_require是python包本身和pip正确地解决了所有问题。简单pip install my_package很有魅力。

设置:现在,作为一个额外的(让我们称之为extra1)我需要调用非python库的二进制文件。X天龙八部

模块X本身(源代码)被添加到my_package代码库,并包括在分发版中my_package...。对我来说,被利用是很遗憾的,X首先需要编译成目标机器上的二进制文件(C++实现;我假设这种编译应该在my_package安装)。有一个MakefileX库为不同的平台编译优化,因此所需的全部工作就是运行make在各自的目录中X图书馆my_package生成进程运行时。

问题1*如何运行终端命令(即,make在包的构建过程中,使用setuptools/distutils?

问题2*如何确保只有在相应的extra1是在安装过程中指定的吗?

例子:

  1. 如果有人跑pip install my_package,没有额外的图书馆汇编。X都会发生。
  2. 如果有人跑pip install my_package [extra1]、模块X需要进行编译,以便在目标机器上创建和使用相应的二进制文件。
EN

回答 1

Stack Overflow用户

发布于 2018-03-16 08:47:38

不幸的是,围绕setup.py和pip之间的交互,文档非常稀少,但是您应该能够这样做:

import subprocess

from setuptools import Command
from setuptools import setup


class CustomInstall(Command):

    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        subprocess.call(
            ['touch',
             '/home/{{YOUR_USERNAME}}/'
             'and_thats_why_you_should_never_run_pip_as_sudo']
        )

setup(
    name='hack',
    version='0.1',
    cmdclass={'customcommand': CustomInstall}
)

这为您提供了使用命令运行任意代码的挂钩,并且还支持各种自定义选项解析(未在这里演示)。

把这个放进setup.py归档并尝试如下:

pip install --install-option="customcommand" .

请注意,此命令已执行。主要的安装顺序,所以取决于您要做的是什么,它可能无法工作。请参见详细的pip安装输出:

(.venv) ayoon:tmp$ pip install -vvv --install-option="customcommand" .
/home/ayoon/tmp/.venv/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / -
-global-options / --install-options.                                                                                                                        
  cmdoptions.check_install_build_global(options)
Processing /home/ayoon/tmp
  Running setup.py (path:/tmp/pip-j57ovc7i-build/setup.py) egg_info for package from file:///home/ayoon/tmp
    Running command python setup.py egg_info
    running egg_info
    creating pip-egg-info/hack.egg-info
    writing pip-egg-info/hack.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/hack.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/hack.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/hack.egg-info/SOURCES.txt'
    reading manifest file 'pip-egg-info/hack.egg-info/SOURCES.txt'
    writing manifest file 'pip-egg-info/hack.egg-info/SOURCES.txt'
  Source in /tmp/pip-j57ovc7i-build has version 0.1, which satisfies requirement hack==0.1 from file:///home/ayoon/tmp
Could not parse version from link: file:///home/ayoon/tmp
Installing collected packages: hack
  Running setup.py install for hack ...     Running command /home/ayoon/tmp/.venv/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-j57ovc7
i-build/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --
record /tmp/pip-_8hbltc6-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ayoon/tmp/.venv/include/site/python3
.6/hack customcommand                                                                                                                                       
    running install
    running build
    running install_egg_info
    running egg_info
    writing hack.egg-info/PKG-INFO
    writing dependency_links to hack.egg-info/dependency_links.txt
    writing top-level names to hack.egg-info/top_level.txt
    reading manifest file 'hack.egg-info/SOURCES.txt'
    writing manifest file 'hack.egg-info/SOURCES.txt'
    Copying hack.egg-info to /home/ayoon/tmp/.venv/lib/python3.6/site-packages/hack-0.1-py3.6.egg-info
    running install_scripts
    writing list of installed files to '/tmp/pip-_8hbltc6-record/install-record.txt'
    running customcommand
done
  Removing source in /tmp/pip-j57ovc7i-build
Successfully installed hack-0.1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100004267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档