清理setup.py中的构建目录

问题描述 投票:0回答:4

如何让我的

setup.py
预删除和后删除构建目录?

python build distutils
4个回答
148
投票

如果我没记错的话,您需要使用

--all
标志来删除
build/lib
之外的内容:

python setup.py clean --all

文档:docs.python.org/3.8/distutils/apiref.html#module-distutils.command.clean


15
投票

对于预删除,只需在调用 setup 之前使用

distutils.dir_util.remove_tree
将其删除即可。

对于删除后,我假设您只想在选定的命令之后进行删除后。子类化相应的命令,重写其 run 方法(在调用基本运行后调用remove_tree),并将新命令传递到 setup 的 cmdclass 字典中。


13
投票

这会在安装之前清除构建目录

python setup.py clean --all install

但是根据你的要求:这将在之前和之后完成

python setup.py clean --all install clean --all

6
投票

这是一个将 Martin 答案的编程方法与 Matt 答案的功能相结合的答案(

clean
,负责所有可能的构建区域):

from distutils.core import setup
from distutils.command.clean import clean
from distutils.command.install import install

class MyInstall(install):

    # Calls the default run command, then deletes the build area
    # (equivalent to "setup clean --all").
    def run(self):
        install.run(self)
        c = clean(self.distribution)
        c.all = True
        c.finalize_options()
        c.run()

if __name__ == '__main__':

    setup(
        name="myname",
        ...
        cmdclass={'install': MyInstall}
    )
© www.soinside.com 2019 - 2024. All rights reserved.