3

I have a python package which contains a C++ extension.

The C++ extension is built within setup.py using its own Makefile, and the .so files are create in its own subfolder, and then copied in the build folder.

When I call python setup.py clean, only the build directory is removed, but the cxxextension/build is not removed, so if I build it again, it is just copied and not recompiled.

How can I instruct setup.py clean to also remove my cxxextension/build folder?

Andrea Zonca
  • 7,329
  • 5
  • 37
  • 64

2 Answers2

0

The accepted answer scares me as the path is relative and extreme care should be taken when deleting anything on user side from python script. You should get your build location with something like:

curdir = os.path.dirname(os.path.realpath(__file__))
buildir = curdir+os.sep+"build"

Once you build a path a bit more safely you can pass the dir to shutil.rmtree.

P.S. better ways to get current path safely are here How do I get the path and name of the file that is currently executing?

Christian Sarofeen
  • 2,095
  • 7
  • 18
0

The easiest way would probably be to simply process it manually in setup.py. You could add something like this to it before calling distutils.setup:

import sys, shutil
if 'clean' in sys.argv:
    shutil.rmtree('cxxextension/build')
aquavitae
  • 14,088
  • 7
  • 53
  • 99