Cleaning build directory in setup.py

PythonBuildDistutils

Python Problem Overview


How could I make my setup.py pre-delete and post-delete the build directory?

Python Solutions


Solution 1 - Python

Does this answer it? IIRC, you'll need to use the --all flag to get rid of stuff outside of build/lib:

python setup.py clean --all

Solution 2 - Python

For pre-deletion, just delete it with distutils.dir_util.remove_tree before calling setup.

For post-delete, I assume you only want to post-delete after selected commands. Subclass the respective command, override its run method (to invoke remove_tree after calling the base run), and pass the new command into the cmdclass dictionary of setup.

Solution 3 - Python

This clears the build directory before to install

python setup.py clean --all install

But according to your requirements: This will do it before, and after

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

Solution 4 - Python

Here's an answer that combines the programmatic approach of Martin's answer with the functionality of Matt's answer (a clean that takes care of all possible build areas):

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}
    )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRam RachumView Question on Stackoverflow
Solution 1 - PythonMatt BallView Answer on Stackoverflow
Solution 2 - PythonMartin v. LöwisView Answer on Stackoverflow
Solution 3 - PythonAdrian LopezView Answer on Stackoverflow
Solution 4 - PythonAlanView Answer on Stackoverflow