You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid generating metadata in pip download --no-deps ... #1884
"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.
Reacted by Jeff, Seppo Yli-Olli, Malcolm Smith, Patrick Decat, Wim Jeantine-Glenn, Eric Bigelow, Joshua Hendinata, JavaCodePhucNV, Frederick Wagner, Marcelo Duarte Trevisani and 14 more
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.
@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.
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
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)
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
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. :)
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.
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.
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:
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.
Locking as there's nothing immediately actionable here, and the ecosystem-level changes needed to improve the situation are already being worked on elsewhere.
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. :)
Activity
xolox commentedon Jul 13, 2014
The
python setup.py egg_infocommand 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 tosetup.py). The setuptoolssetup_requiresfeature 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 withsetup_requires:Setuptools downloads and unpacks setup requirements on demand every time a
setup.pyscript 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 --helporpython setup.py --version.As a bonus setuptools uses
easy_installfor setup requirements which can cause several problems: Usingpip install --no-indexwill not stopeasy_installfrom downloading remote package archives and usingpip install --index-url=...will not instructeasy_installto 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 withsetup.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.pyscript).It is a shame that the
setup_requiresfeature 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.sholsapp commentedon Nov 19, 2014
@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 ofOSErrorsabout paths not existing, presumably because invocations ofsetup.py egg_infoon 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.
dstufft commentedon Mar 24, 2017
When we implement this, we should take care to prevent path traversal bugs like mentioned in #731.
pradyunsg commentedon Jun 29, 2017
@dstufft Can this be closed since the
--downloadflag has been removed?dstufft commentedon Jun 29, 2017
I think the issue is still valid for
pip download --no-deps?pradyunsg commentedon Jun 29, 2017
I guess so.
pdecat commentedon Jun 6, 2018
What about not running
setup.py egg_infoonpip install --no-deps? (Even without--no-download)That would allow working around a current pylxd issue canonical/pylxd#308 (comment)
RonnyPfannschmidt commentedon Jun 6, 2018
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
RonnyPfannschmidt commentedon Jun 15, 2018
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)
RonnyPfannschmidt commentedon Jun 16, 2018
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
RonnyPfannschmidt commentedon Jun 16, 2018
for an sdist the internals will aways fetch a
class IsSDist(DistAbstraction)frompip._interal.operations.prepareits
prep_for_distwill always runegg_infoand assert thatassert_source_matches_versionwith 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
Work around pypa/pip#1884
40 remaining items
takluyver commentedon Oct 12, 2021
No problem, I'm aware that PEP 621 usage is going to be pretty small until setuptools supports it. 🙂
pradyunsg commentedon Oct 12, 2021
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. :)
pradyunsg commentedon Oct 12, 2021
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.
pfmoore commentedon Oct 12, 2021
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.tomland 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 inpyproject.toml, but different metadata in (say)setup.cfgis weird and broken, but nevertheless it's a good demonstration of why pip needs to leave metadata generation to the backend.tvalentyn commentedon Oct 12, 2021
Just a note that adding
--no-build-isolationdoes not necessarily disable metadata verification after package is downloaded. As a result, both:fail for me in a clean environment where
Cython(a build dependency) is not installed.pfmoore commentedon Oct 12, 2021
That's probably because even with
--no-depswe still need to (check for and) satisfyPython-Requiresmetadata.wimglenn commentedon Oct 12, 2021
@pfmoore It still does it even if you add
--ignore-requires-python.pradyunsg commentedon Oct 13, 2021
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-isolationmeans 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.th0ma7 commentedon Dec 18, 2021
Making some mileage on #1884 (comment) from @pfmoore I ended with the following which seems to work all right
EDIT: Added ability to
.tar.gzand.zipsource packagespfmoore commentedon Aug 25, 2022
Locking as there's nothing immediately actionable here, and the ecosystem-level changes needed to improve the situation are already being worked on elsewhere.
pradyunsg commentedon Aug 25, 2022
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. :)
pfmoore commentedon Aug 25, 2022
As noted in an earlier comment of yours an easy way to restart discussion is if someone creates a PR with a suggested solution 🙂