如何删除构建产品 [英] How to remove build products
问题描述
是否可以自动删除基于 setuptools 的 setup.py
脚本生成的生成产品?
Is it possible to automatically remove build products that are generated by a setup.py
script based on setuptools?
我刚刚开始一个新的Python项目,这是我第一次使用 setuptools 作为开发人员,所以我可能会出错。当我使用 python setup.py bdist
构建项目时,三个目录分别是 build
, dist
和一个以 .egg-info
结尾的文件。然后,当我运行 python setup.py clean
时,它似乎什么都没做,只是打印以下内容:
I've just started with a new Python project and it's the first time I'm using setuptools as a developer, so I may be getting something wrong. when I build the project using python setup.py bdist
, three directories, build
, dist
and one ending in .egg-info
are created. When I then run python setup.py clean
it doesn't seem to be doing anything and just prints this:
running clean
我尝试添加-全部
到 clean
命令,尽管它确实删除了 build $ c $中的某些文件c>目录,它不会删除目录本身或其他两个目录中的任何文件。
I've tried adding --all
to the clean
command and while it does remove some files in the build
directory, it doesn't remove the directory itself or any of the files in the other two directories.
我假设这样做应该很容易,并且我只是在错误的地方看。我已经习惯了此功能,例如来自几乎任何使用 make
的项目,其中 make clean
或 make distclean
将删除所有构建产品。
I'm assuming that this should be possible very easily and that I'm just looking in the wrong place. I'm used to this functionality e.g. from virtually any project using make
where make clean
or make distclean
will remove any build products.
推荐答案
标准方法:
distutils.command.clean
此命令删除由build及其子命令创建的临时文件,例如中间编译的目标文件。使用--all选项,将删除完整的构建目录。
This command removes the temporary files created by build and its subcommands, like intermediary compiled object files. With the --all option, the complete build directory will be removed.
另一个做到这一点的方法:
Another way to do it:
这可能不是最佳解决方案:
从评论下面的答案看来 python setup.py clean --all
有时无法删除所有内容(注释示例中的Numpy)。
It seems from the comment below this answer that python setup.py clean --all
sometimes fails to remove everything (Numpy in the example in the comment).
似乎不是所有的setup.py脚本都支持干净。例如:NumPy – kevinarpe 16年6月15日在7:14
It seems not all setup.py scripts support clean. Example: NumPy – kevinarpe Jun 15 '16 at 7:14
remove_tree()
在安装脚本中的命令:
You could use the remove_tree()
command in your setup script:
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 和 glob ):
import shutil, glob
shutil.rmtree('dist')
shutil.rmtree(glob.glob('*.egg-info')[0])
shutil.rmtree(glob.glob('build/bdist.*')[0])
将其添加到安装脚本中比较困难,但是使用这个答案,应该看起来像这样:
Adding it to the setup script was harder but using this answer, it should look something like this:
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'}
允许此类在安装后运行。 有关更多详细信息,请参见此答案。
cmdclass = {'install'}
allows this class to run after the installation. See this answer for more details.
是什么使我有了使用shutil的想法?
使用shutil的想法来自旧的文档:
The idea to use shutil came from the old documentation:
其中一些可以被shutil模块取代吗?
Some of this could be replaced with the shutil module?
这篇关于如何删除构建产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!