Skip to content

Avoid generating metadata in pip download --no-deps ... #1884

Open

Description

@tlynn

"pip install xxx --download yyy --no-deps" runs "python setup.py egg_info" after downloading (to generate the requires.txt dependency list?). This seems unnecessary.

This is a problem in the case of "cryptography", since its setup.py uses setup_requires, which automatically downloads its dependencies at the slightest nudge, even just --help (cryptography issue #716).

This impedes using pip install --download to build a collection of trusted (checksummed) modules for later installation.

Activity

  1. xolox commented on Jul 13, 2014

    @xolox

    The python setup.py egg_info command is run for metadata discovery. It's an unfortunate fact of the Python packaging ecosystem that anything related to packaging always involves arbitrary code execution (referring to setup.py). The setuptools setup_requires feature is a particularly nasty example of this (don't get me wrong, I understand it has its uses, but it's still nasty). So I believe this should be fixed on the side of the cryptography package, because we can't expect pip to reasonably deal with setup_requires:

    • Setuptools downloads and unpacks setup requirements on demand every time a setup.py script is evaluated in any way, even if you run something very simple which seems like it should be a read only operation, e.g. python setup.py --help or python setup.py --version.

      As a bonus setuptools uses easy_install for setup requirements which can cause several problems: Using pip install --no-index will not stop easy_install from downloading remote package archives and using pip install --index-url=... will not instruct easy_install to use the custom index URL, instead the URL has to be set in ~/.pydistutils.cfg (you can't even configure it using an environment variable).

    • I assume setuptools always installs setup requirements as the very first thing because setup requirements can for example "install" custom setup commands that should appear in the output of python setup.py --help-commands. Setuptools can't decide what setup requirements will do (the possibilities are unbounded :-) so setuptools errs on the side of caution and always installs setup requirements before doing anything else with setup.py.

    • The only people in a position to judge which setup commands actually need setup requirements are the developers of the package that defines setup requirements. For this reason I created pull request Work around for side effects in setup.py script pyca/cryptography#1257 to get this fixed on the side of the cryptography package (it seems like it's possible to fix this with a not-too-big change to the setup.py script).

    It is a shame that the setup_requires feature of setuptools is so rudimentary, it would have been nice if there was a canonical and supported way to tell setuptools which setup commands really need setup requirements. That might have avoided this mess.

  2. sholsapp commented on Nov 19, 2014

    @sholsapp

    @xolox thanks for the detailed description, that helped me understand what was going on here.

    Another unfortunate side effect of the behavior reported by @tlynn is that we can't run parallel downloads for packages that share transitive dependencies. What one sees is that when using something like
    pip install --exists-action=i --download-dir ./download-cache <lots of dependencies that share transitive dependencies> are lots of OSErrors about paths not existing, presumably because invocations of setup.py egg_info on packages that are in the process of being downloaded result in files not being found.

    This is probably a convoluted example, but I figured I'd share my use case here.

  3. modified the milestone: 6.0 on Dec 18, 2014
  4. dstufft commented on Mar 24, 2017

    @dstufft
    Member

    When we implement this, we should take care to prevent path traversal bugs like mentioned in #731.

  5. pradyunsg commented on Jun 29, 2017

    @pradyunsg
    Member

    @dstufft Can this be closed since the --download flag has been removed?

  6. dstufft commented on Jun 29, 2017

    @dstufft
    Member

    I think the issue is still valid for pip download --no-deps?

  7. pradyunsg commented on Jun 29, 2017

    @pradyunsg
    Member

    I guess so.

  8. pdecat commented on Jun 6, 2018

    @pdecat

    What about not running setup.py egg_info on pip install --no-deps ? (Even without --no-download)

    That would allow working around a current pylxd issue canonical/pylxd#308 (comment)

  9. RonnyPfannschmidt commented on Jun 6, 2018

    @RonnyPfannschmidt
    Contributor

    how about additionally not running egg-info if the tarball ships an egg_info file to begin with
    i believe these days the setuptools-generated packages can be trusted

    however this should be introduced over a grace period where pip warns about differences between shipped and freshly generated

  10. RonnyPfannschmidt commented on Jun 15, 2018

    @RonnyPfannschmidt
    Contributor

    i just ran into this when trying to use pip as sane caching downloader of other stuff
    (its just really neat for travis use since the download uses the pip cache which is cached + all the bells and whistles)

  11. RonnyPfannschmidt commented on Jun 16, 2018

    @RonnyPfannschmidt
    Contributor

    i just tried to disable this for downloading and the issue spans resolver, preparer and a few other utilities - i will need to retry with more time

  12. RonnyPfannschmidt commented on Jun 16, 2018

    @RonnyPfannschmidt
    Contributor

    for an sdist the internals will aways fetch a class IsSDist(DistAbstraction) from pip._interal.operations.prepare

    its prep_for_dist will always run egg_info and assert that assert_source_matches_version

    with my limited understanding of the internals this is no longer a small saturday morning hack but rather a more nuanced re-factoring and i have to pull out

  13. added a commit that references this issue on Sep 4, 2018
  14. 40 remaining items

  15. takluyver commented on Oct 12, 2021

    @takluyver
    Member

    No problem, I'm aware that PEP 621 usage is going to be pretty small until setuptools supports it. 🙂

  16. pradyunsg commented on Oct 12, 2021

    @pradyunsg
    Member

    PEP 621 usage is going to be pretty small until setuptools supports it. 🙂

    Yea, and pypa/setuptools#1688 is sitting, waiting for someone to pick up and implement OR to provide funding for doing the necessary work on it to the PSF's Packaging-WG. :)

  17. pradyunsg commented on Oct 12, 2021

    @pradyunsg
    Member

    FWIW, a big part of the problem here is the implementation details of working within pip's codebase.

    There's a concept of "preparing" a requirements, which involves downloading them and generating metadata for them. There's no separation between the two, and detangling these would likely be a non-trivial amount of work. @nchepanov (with some help from me) spent a bunch of time investigating this in Dev 2020, and the notes from that work are available above, in comments on this issue.

  18. pfmoore commented on Oct 12, 2021

    @pfmoore
    Member

    Yea, and pypa/setuptools#1688 is sitting, waiting for someone to pick up

    Apologies, I got confused between PEP 621 (Storing project metadata in pyproject.toml) and PEP 643 (Metadata for Package Source Distributions). PEP 621 is irrelevant here, as tools should not read metadata from pyproject.toml. Reading metadata from a sdist via PEP 643 would be useful, and is valid, though.

    While I guess it's tempting to assume that pip can read pyproject.toml and if it finds PEP 621 data, then use it, it would be wrong because there's no guarantee that the backend supports PEP 621, so there's no reason to believe that the metadata in the generated wheel/sdist would bear any relationship to the PEP 621 data. Obviously a project that specifies metadata in pyproject.toml, but different metadata in (say) setup.cfg is weird and broken, but nevertheless it's a good demonstration of why pip needs to leave metadata generation to the backend.

  19. tvalentyn commented on Oct 12, 2021

    @tvalentyn

    As well as setup_requires, this also affects pyproject.toml. pip download --no-deps will download and install anything listed in there. You can stop this by passing --no-build-isolation, but that shouldn't be necessary because the command didn't ask for a build in the first place.

    Just a note that adding --no-build-isolation does not necessarily disable metadata verification after package is downloaded. As a result, both:

    python -m pip download --dest /tmp pyarrow==5.0.0 --no-deps --no-binary :all: --no-build-isolation 
    
    python -m pip download --dest /tmp pyarrow==5.0.0 --no-deps --no-binary :all: --no-build-isolation --no-use-pep517
    

    fail for me in a clean environment where Cython (a build dependency) is not installed.

  20. pfmoore commented on Oct 12, 2021

    @pfmoore
    Member

    That's probably because even with --no-deps we still need to (check for and) satisfy Python-Requires metadata.

  21. wimglenn commented on Oct 12, 2021

    @wimglenn
    Contributor

    @pfmoore It still does it even if you add --ignore-requires-python.

  22. pradyunsg commented on Oct 13, 2021

    @pradyunsg
    Member

    fail for me in a clean environment where Cython (a build dependency) is not installed.

    Please see https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/#build-isolation and https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/#disabling-build-isolation.

    Passing --no-build-isolation means that you'll manage the build-time dependencies yourself. It doesn't change that pip still performs metadata generation -- you will still need those build-time dependencies to be installed.

  23. added
    C: build logicStuff related to metadata generation / wheel generation
    and removed on Oct 13, 2021
  24. th0ma7 commented on Dec 18, 2021

    @th0ma7

    Making some mileage on #1884 (comment) from @pfmoore I ended with the following which seems to work all right

    while IFS= read -r requirement
    do
       query="curl -s https://pypi.org/pypi/${requirement%%=*}/json"
       query+=" | jq -r '.releases[][]"
       query+=" | select(.packagetype==\"sdist\")"
       query+=" | select((.filename|test(\"-${requirement##*=}.tar.gz\")) or (.filename|test(\"-${requirement##*=}.zip\"))) | .url'"
    
       # Get local filename from query
       localFile=$(basename $(eval ${query} 2>/dev/null) 2>/dev/null)
    
       # Download file
       if [ "${localFile}" != "" ]; then
          wget --secure-protocol=TLSv1_2 -nv -O ${localFile}.part -nc $(eval $query)
          mv ${localFile}.part ${localFile}
       else
          echo "ERROR: Invalid package name [${requirement%%=*}]"
       fi
    done < <(grep -v -e "^$" -e "^#" requirements.txt)
    

    EDIT: Added ability to

    1. remove commented + blank lines
    2. capturing both .tar.gz and .zip source packages
    3. echo error when filename isn't ok
  25. pfmoore commented on Aug 25, 2022

    @pfmoore
    Member

    Locking as there's nothing immediately actionable here, and the ecosystem-level changes needed to improve the situation are already being worked on elsewhere.

  26. locked and limited conversation to collaborators on Aug 25, 2022
  27. pradyunsg commented on Aug 25, 2022

    @pradyunsg
    Member

    Well... it's possible to do this purely on pip's end (see #1884 (comment) and discussion immediately after that).

    The bit that's missing is developer resources to actually do that work, since that's closely related with how our build logic only has a single "prepare" operation that maps directly to "fetch file + generate metadata". That's something we've been chipping away at since mid 2019 (#6607, #7049).

    None the less, I think it's reasonable to leave this locked for a bit. :)

  28. pfmoore commented on Aug 25, 2022

    @pfmoore
    Member

    As noted in an earlier comment of yours an easy way to restart discussion is if someone creates a PR with a suggested solution 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: build logicStuff related to metadata generation / wheel generationresolution: deferred till PRFurther discussion will happen when a PR is madetype: bugA confirmed bug or unintended behavior

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions