标准方法:
distutils.command.clean
From 清理 setup.py 中的构建目录 https://stackoverflow.com/questions/1594827/cleaning-build-directory-in-setup-py/1594896#1594896和文档 https://docs.python.org/3/distutils/apiref.html#module-distutils.command.clean:
此命令删除由 build 及其子命令创建的临时文件,例如中间编译的目标文件。使用 --all 选项,将删除完整的构建目录。
另一种方法:
这可能不是最好的解决方案:
从评论看来在这个答案下面 https://stackoverflow.com/questions/1594827/cleaning-build-directory-in-setup-py/1594896#1594896 that python setup.py clean --all
有时无法删除所有内容(评论中示例中的 Numpy)。
似乎并非所有 setup.py 脚本都支持 clean。示例:NumPy – kevinarpe 2016 年 6 月 15 日 7:14
你可以使用remove_tree() https://docs.python.org/2.5/dist/module-distutils.dirutil.html设置脚本中的命令:
import glob
remove_tree(['dist', glob.glob('*.egg-info')[0],glob.glob('build/bdist.*')[0]])
或者在设置脚本中:
from setuptools import setup
from setuptools.command import install
class PostInstallCommand(install):
def run(self):
import glob
from distutils.dir_util import remove_tree
remove_tree(['dist', glob.glob('*.egg-info')[0],glob.glob('build/bdist.*')[0]])
setup(name='Some Name',
version='1.0',
description='A cross platform library',
author='Simon',
platforms = ["windows", "mac", "linux"],
py_modules = ['UsefulStuff', '__init__'],
cmdclass = {'install':PostInstallCommand}
)
我解决这个问题的另一种方法是手动删除所有内容(使用shutil https://docs.python.org/3/library/shutil.html and glob https://docs.python.org/3/library/glob.html):
import shutil, glob
shutil.rmtree('dist')
shutil.rmtree(glob.glob('*.egg-info')[0])
shutil.rmtree(glob.glob('build/bdist.*')[0])
将其添加到设置脚本中比较困难,但是使用这个答案 https://stackoverflow.com/questions/20288711/post-install-script-with-python-setuptools/36902139#36902139,它应该看起来像这样:
from setuptools import setup
from setuptools.command import install
class PostInstallCommand(install):
def run(self):
import shutil, glob
shutil.rmtree('dist')
shutil.rmtree(glob.glob('*.egg-info')[0])
shutil.rmtree(glob.glob('build/bdist.*')[0])
install.run(self)
setup(name='Some Name',
version='1.0',
description='A cross platform library',
author='Simon',
platforms = ["windows", "mac", "linux"],
py_modules = ['UsefulStuff', '__init__'],
cmdclass = {'install':PostInstallCommand}
)
cmdclass = {'install'}
允许此类在安装后运行。请参阅此答案了解更多详细信息 https://stackoverflow.com/questions/20288711/post-install-script-with-python-setuptools/36902139#36902139.
是什么让我产生了使用 Shutil 的想法?
使用 Shutil 的想法来自于旧的文档 https://docs.python.org/2.5/dist/module-distutils.dirutil.html:
其中一些可以用shutil模块替换?