setuptools: setup.py leaves build, dist, .egg-info etc + even clean doesn't remove them

By default, setup.py leaves build, dist, ${project_name}.egg-info directories in a projects directory.

I’m not convinced these need to be here these days, certainly feels messy to have these.

Putting all this output into one directory would be some improvement. But maybe there’s a better solution ?

python setup.py clean doesn’t even delete these without adding hacks.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 60
  • Comments: 17

Commits related to this issue

Most upvoted comments

Rather than trying to clean up files from the source directory, would it be better to be able to do out-of-source package builds? Something like this: cd /tmp/build && python /path/to/setup.py. That way those generated files never touch the source directory in the first place.

@maphew I suggest that it be added as functionality (it doesn’t work that way currently), then documented as a best practice. It shouldn’t break the way it’s done now.

Trying to make a nice workaround for this stuff, I couldn’t manage with changing workdir - it fails with line “error: package directory ‘my_root_package’ does not exist”. In my case I just build a source distribution with no data files, extensions etc, just plain python archive. I found a workaround for this issue - having workdir at repo root as usual, use command: python setup.py egg_info --egg-base tmp/build sdist --dist-dir tmp/dist

How it works:

  • sdist anyway runs egg_info command, all commands run only once, so you can freely add all the commands you already use and override their options.
  • egg_info allows for changing the path where .egg-info directory will be written, so this way I get rid of .egg-info files in source root.
  • –dist-dir thing changes the destination for output archive.
  • All others artifacts are cleaned up by default.
  • If you have more complex builds let’s say with extensions, most probably you should also be able to override their options by this method.

Not really convenient solution, but it does not require any extra code.

It would be very nice if the behaviour of clean in setuptools could be improved; the current behaviour is really annoying IMHO.

I completely agree. I find it very strange that there is no option to select a different output folder AND that the clean command does not really remove the generated artifacts.

I think a out-of-source build and proper clean should be natively supported by setuptools, as per previous comments.

Like others who’ve commented here, I have had to resort to a custom clean command to remove the dist directory.

It would be good if this could be fixed in setuptools. A user would reasonably expect that python setup.py clean --all would clean all directories. However, I haven’t come across a situation where I want a partial clean so I’m not sure there is much point in a --all flag: clean should just clean everything.

class CleanCommand(clean):
    """
    Custom implementation of ``clean`` setuptools command."""

    def run(self):
        """After calling the super class implementation, this function removes
        the dist directory if it exists."""
        self.all = True  # --all by default when cleaning
        super().run()
        if exists("dist"):
            log.info("removing 'dist' (and everything under it)")
            rmtree("dist")
        else:
            log.warn("'dist' does not exist -- can't clean it")

I’ve got setupext-janitor working and issued a PR to the base master https://github.com/dave-shawley/setupext-janitor/pull/14

Other solutions I’ve found which have been refined a stage above “here’s what’s worked for me” code snippets here and on stack overflow:

If you find an easy-to-use built-in solution, it would be great to post it here as well: https://stackoverflow.com/questions/64952572/output-directories-for-python-setup-py-sdist-bdist-wheel Thanks!