11

Invoking a call to setup from setuptools with version 0.9.6 I can run the following command:

python setup.py clean

But what does this exactly do? The help on this command is a bit sparse, and running

python setup.py clean --all

gives useless statements like

'build/lib.linux-i686-2.7' does not exist -- can't clean it

Is there a possibility to use this clean command to e.g. cleanup automatically temporary python files ending in .pyc and .~? Can this be done with this command, does it need to be configured, ...?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • Out of interest, why would you want the extra behaviour? I can imagine it feels nice to have a clean directory, but it's not something you should be bothered with overall. Any specific reason? –  Jul 23 '13 at 09:12
  • I want the extra behavior in order to specify simply a directory, from which the whole content is taken into the package. Without removing temporary python files first, all of those temporary files will be used in the packing, no? Also, the `clean` command can be used to simply clean the working directory from temporary files to clearly see what you have. – Alex Jul 24 '13 at 13:11
  • For packaging, look into using `setup.py sdist`. As for cleaning up: `*.pyc` files shouldn't really be a problem, but you could consider `find . -name "*.pyc" | xargs rm`. Those `*~` files aren't something Python should be bothered with, as those are unrelated to Python (this may go for a few other file types as well). –  Jul 24 '13 at 17:00
  • Using `xargs` this way is dangerous. use `find -delete` instead. – mic_e Oct 15 '14 at 15:42
  • @mic_e Off-topic, but could you shortly explain why that's dangerous? Or provide (a) link(s) perhaps? –  Oct 15 '14 at 20:42
  • 1
    Imagine there's a file that contains spaces: `a b`. instead, the files `a` and `b` will be removed. Even worse: Imagine a file `-rf .`. Instead, the entire directory will be removed. A safer way to use `xargs` would be `find -print0 | xargs -0 rm --`, which would perform the right task in both scenarios, but `find -delete` is the easiest. – mic_e Oct 16 '14 at 08:16
  • See pypa/setuptools Issue 1347 for discussion and possible workarounds - https://github.com/pypa/setuptools/issues/1347. Also https://stackoverflow.com/questions/28264894/how-to-remove-build-products and https://stackoverflow.com/questions/1594827/cleaning-build-directory-in-setup-py – matt wilkie Jan 15 '19 at 22:08

1 Answers1

7

As far as I know, it just removes the the build subdirectory, where Python puts all the files to be installed, including extensions that need to be compiled.

There shouldn't be any *.pyc files elsewhere, unless you ran Python on some scripts in the source directory (which may happen if you run tests), or imported modules directly from the source directory. *~ files are emacs backup files, and thus wouldn't be cleaned up by setup.py anyway. (If you've seen this behaviour from make clean, than that's simply because someone coded that into the Makefile.)

You probably could override the clean command in a way to (recursively) remove *.pyc files, but I doubt if there's a need. In general, Python is smart enough to recompile *.py files into *.pyc files if the former have changed, and otherwise using the latter will simply be faster.

There is one caveat I've come across, and that is when doing a setup.py build_ext --inplace, cleaning won't remove the compiled module(s), since these are not in the build directory. Which to me feels like shortcoming of the clean command.

Overall, it looks like the clean command was added to be in line with make's behaviour, but it doesn't seem to add much.

  • 1
    Please see other comment: When I create a package, I want to get rid of temporary files first. I do not want them to be part of the package. – Alex Jul 24 '13 at 13:13
  • If I did use `setup.py build_ext --inplace`, is there a way to remove all the compiled modules? – Garrett Nov 30 '14 at 08:20
  • 2
    @Garrett If these are just pure Python modules, you could use the (mofidied) find suggestion in the comments to the question: `find -name "*pyc" -delete`. If there are other files (compiled from Cython or C source files), things are hairier and you can try to run the find command for each file type. –  Nov 30 '14 at 16:45
  • 3
    "As far as I know" - can you confirm you actually *tried* `python setup.py clean` before posting this answer? For me it doesn't remove `build/` unless I use `--all`! – Adam Spiers Jun 12 '16 at 09:03