[英]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.