Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stuaxo opened this issue May 2, 2018 · 17 comments
Open
Labels
Needs Triage Issues that need to be evaluated for severity and status.

Comments

@stuaxo
Copy link

stuaxo commented May 2, 2018

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.

@stuaxo
Copy link
Author

stuaxo commented May 9, 2018

I end up putting this in every setup.py

class CleanCommand(Command):
    """Custom clean command to tidy up the project root."""
    CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info'.split(' ')

    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        global here

        for path_spec in self.CLEAN_FILES:
            # Make paths absolute and relative to this path
            abs_paths = glob.glob(os.path.normpath(os.path.join(here, path_spec)))
            for path in [str(p) for p in abs_paths]:
                if not path.startswith(here):
                    # Die if path in CLEAN_FILES is absolute + outside this directory
                    raise ValueError("%s is not a path inside %s" % (path, here))
                print('removing %s' % os.path.relpath(path))
                shutil.rmtree(path)

@stuaxo stuaxo changed the title setup.py leaves loads of directories around. setup.py clean doesn't remove build, dist, .egg-info etc May 9, 2018
@stuaxo stuaxo changed the title setup.py clean doesn't remove build, dist, .egg-info etc setup.py leaves build, dist, .egg-info etc + even clean doesn't remove them May 9, 2018
@pganssle pganssle added the Needs Triage Issues that need to be evaluated for severity and status. label Oct 19, 2018
@maphew
Copy link

maphew commented Jan 15, 2019

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:

@maphew
Copy link

maphew commented Feb 6, 2019

I've made good headway with setupext-janitor starting from @ionelmc's fork https://github.com/ionelmc/setupext-janitor which fixes namespace. There's a blocking issue though on Windows, ... clean --dist exits early with "error: don't know how to create RPM distributions on platform nt" (dave-shawley/setupext-janitor#12). I'll post progress there as I can, label:help-wanted though as I'm out of my depth. ;-)

@stuaxo
Copy link
Author

stuaxo commented Feb 7, 2019

It might be worth opening a PR marked WIP so we can easily see the changes.

@maphew
Copy link

maphew commented Feb 7, 2019

@stuaxo Sorry, I don't follow. A PR here in setuptools?

I've identified where the error occurs in setuptools now, but don't know how to proceed. dave-shawley/setupext-janitor#12 (comment)

@stuaxo
Copy link
Author

stuaxo commented Feb 7, 2019

Sorry for the noise, was replying on my phone in a hurry :)

@maphew
Copy link

maphew commented Feb 8, 2019

I've got setupext-janitor working and issued a PR to the base master dave-shawley/setupext-janitor#14

@cmarquardt
Copy link

It would be very nice if the behaviour of clean in setuptools could be improved; the current behaviour is really annoying IMHO. @maphew's updated version of setupext-janitor works nicely (thanks!). However, the original owner/maintainer (@dave-shawley) doesn't seem to be much interested any more (?), unfortunately.

Is there a chance to get the patched version released on Pypi somehow? Or even be integrated into setuptools properly?

@gary-jackson
Copy link

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
Copy link

maphew commented May 11, 2019

@cmarquardt these and other recent comments have woken up setupext-janitor and a recent version has been published to pypi. @dave-shawley wasn't receiving notifications and didn't know there was any interest or significant downstream use.

@gary-jackson are you suggesting this be something setup.py does automatically or as documenting best practice instructions?

@stuaxo
Copy link
Author

stuaxo commented Jul 25, 2019

Not speaking for @gary-jackson but I'd like it to do this automatically, not sure if it will break someones build process though ? OTOH not leaving random files around has to be a pretty good benefit.

@gary-jackson
Copy link

@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.

ghost pushed a commit to torproject/stem that referenced this issue Apr 2, 2020
Unfortunately setuptools creates a superfluous 'dist' directory that isn't
removed with clean...

  pypa/setuptools#1347

Working around the test failure it causes...

  ======================================================================
  FAIL: test_sdist
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/home/atagar/Desktop/stem/stem/util/test_tools.py", line 141, in <lambda>
      self.method = lambda test: self.result(test)  # method that can be mixed into TestCases
    File "/home/atagar/Desktop/stem/stem/util/test_tools.py", line 208, in result
      test.fail(self._result.msg)
  AssertionError: /home/atagar/Desktop/stem/dist already exists, maybe you manually ran 'python setup.py sdist'?
ghost pushed a commit to torproject/stem that referenced this issue Apr 10, 2020
Setuptools 'clean' command doesn't work...

  pypa/setuptools#1347

Cleaning up after the turds it creates when we run our tests.
@mfrigerio17
Copy link

mfrigerio17 commented Jul 5, 2020

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.

@faddey-w
Copy link

faddey-w commented Oct 13, 2020

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.

@HMaker
Copy link

HMaker commented Nov 20, 2020

I had problems with that, I added data files to MANIFEST.in, added include_package_data=True to setup() and built the package again (python setup.py sdist) but setuptools won't install the data files until I manually clean /dist and /*.egg-info dirs, past builds were interfering in the behavior of new builds, if that's allowed to happen then setuptools should also clean that directories along with /build (python setup.py clean --all should clean /dist and /*.egg-info too).

@josephernest
Copy link

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!

HorlogeSkynet pushed a commit to HorlogeSkynet/archey4 that referenced this issue Mar 12, 2021
@mcarans
Copy link

mcarans commented Sep 15, 2021

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

archlinux-github pushed a commit to archlinux/aur that referenced this issue Apr 10, 2023
Clean up `dist` directory before starting the build.
This is in order to prevent `error: unrecognized arguments` at
`python -m install` time, which would occur if `dist` happens to contain
a stale wheel from an earlier build.

See also: pypa/setuptools#1347
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Triage Issues that need to be evaluated for severity and status.
Projects
None yet
Development

No branches or pull requests

10 participants