简体   繁体   English

setup.py:在其他任何事情之前运行build_ext

[英]setup.py: run build_ext before anything else

I'm working with a setup.py that creates a whole bunch of SWIG interface files during the build_ext step. 我正在使用setup.py,它在build_ext步骤中创建了一大堆SWIG接口文件。 This needs to run first, because subsequent build steps need a full list of the python files to work properly (like copying the python files to the package directory, creating the egg, creating the sources list, etc.). 这需要先运行,因为后续的构建步骤需要一个完整的python文件列表才能正常工作(比如将python文件复制到包目录,创建egg,创建源列表等)。

This is what currently happens when you do setup.py install : 当你进行setup.py install时,这是当前发生的事情:

running install
running bdist_egg
running egg_info
running install_lib
running build_py
running build_ext

The build_py step tries to copy all the python files that it finds to the build directory. build_py步骤尝试将它找到的所有python文件复制到构建目录。 Those files don't exist until build_ext runs (swig creates a bunch of .py files). build_ext运行之前,这些文件不存在(swig会创建一堆.py文件)。

This answer recommends changing sub_commands but that didn't appear to do anything. 这个答案建议更改sub_commands但似乎没有做任何事情。

I tried just subclassing the install command class like this to run build_ext before anything else: 我试着像这样build_ext class install类,在其他任何事情之前运行build_ext

class Build_ext_first(setuptools.command.install.install):
    def run(self):
        self.run_command("build_ext")
        super(Build_ext_first, self).run()

..and then working it in to setup with cmdclass : ..然后使用cmdclass进行设置:

setup(
    ...
    cmdclass = {'install' : Build_ext_first}
)

But that didn't work because super doesn't work with old-style classes and install apparently doesn't inherit from object . 但是这不起作用,因为super不能用于旧式类,并且install显然不会从object继承。

How do I do build_ext first? 我该如何先做build_ext

For fear of posting on a 2 year old post. 因为害怕张贴在一个2岁的帖子上。 I think the correct way to solve this would be to fix it during the "build" phase: 我认为解决这个问题的正确方法是在“构建”阶段修复它:

from setuptools import setup, find_packages, Extension
from setuptools.command.build_py import build_py as _build_py    

class build_py(_build_py):
    def run(self):
        self.run_command("build_ext")
        return super().run()

setup(...,
    cmdclass = {'build_py' : build_py},
)

That way it works for bdist_wheel as well as install (haven't tested other stuff). 这样它适用于bdist_wheel以及安装(尚未测试其他东西)。

Note the super syntax is a bit different in Python 2: 注意Python 2中的super语法有点不同:

class build_py(_build_py):
    def run(self):
        self.run_command("build_ext")
        return _build_py.run(self)

It appears the old way of doing super() is forwards-compatible so I just did that: 看起来执行super()的旧方法是向前兼容所以我只是这样做:

class Build_ext_first(setuptools.command.install.install):
    def run(self):
        self.run_command("build_ext")
        return setuptools.command.install.install.run(self)


setup(
    ...,
    cmdclass = {'install' : Build_ext_first}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Setup.py Build_Ext --inplace - Python Setup.py Build_Ext --inplace python setup.py build_ext 在 Windows Azure 管道中 - python setup.py build_ext in Windows Azure pipeline 在不提供python setup.py的情况下进行构建-就地构建 - Making build --inplace without providing to python setup.py build_ext 运行 setup.py build_ext 时防止虚假 -m32 标志 - Preventing spurious -m32 flag when running running setup.py build_ext python setup.py build_ext --include-dirs = / usr / include / gdal / not work - python setup.py build_ext --include-dirs=/usr/include/gdal/ not work Python 3:setup.py:执行所有操作的pip install(build_ext + install) - Python 3: setup.py: pip install that does everything (build_ext + install) 命令`python setup.py build_ext --inplace`总是创建一个新目录 - The command `python setup.py build_ext --inplace` always create a new directory 无法将“ python”识别为内部或外部命令python setup.py build_ext --inplace - 'python' is not recognized as an internal or external command python setup.py build_ext --inplace 运行'python setup.py build_ext --inplace'时出错 - Error while running 'python setup.py build_ext --inplace' 调用 setup.py 安装时如何将 --debug 传递给 build_ext? - How to pass --debug to build_ext when invoking setup.py install?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM