问题
I've created a setup.py for my application. Some of the dependencies i set in install_requires require pip version 19.3.1 or greater.
Is there a way to check for pip version as part of setup.py? and to upgrade pip prior to build?
回答1:
This is not your responsibility to build workarounds in your project for the issues in the packaging of other projects. This is kind of a bad practice. There is also not much point in doing this as part of a setup.py
anyway since in many cases this file is not executed during install time.
The best thing you can do is try and fix the faulty packaging of these dependency projects directly: contact the maintainers, file an issue, propose a fix, etc.
The second best thing is to inform the users of your project. Clearly state this problem in the documentation of your own project and how to prevent it (i.e. "install pip version 19.3.1 or greater").
Update:
If you decide to enforce a check in setup.py
anyway, here are some techniques that might help...
But I would still recommend against those, since your setup.py
is not actually at fault here, but the issue seems to lie in the packaging of the dependencies.
1.
__requires__ = ['pip >= 19.3.1'] # make sure it's before 'import setuptools'
import setuptools
setuptools.setup(
# ...
)
This would trigger an exception:
pkg_resources.DistributionNotFound: The 'pip>=19.3.1' distribution was not found and is required by the application
The drawback of this technique is that it doesn't trigger when called from pip (for example: pip install .
), since in that case the __main__
module is not setup.py
but a module of pip.
Reference:
- https://setuptools.readthedocs.io/en/stable/pkg_resources.html?highlight=requires#workingset-objects
2.
import pkg_resources
import setuptools
pkg_resources.require(['pip >= 19.3.1'])
setuptools.setup(
# ...
)
This would trigger a pkg_resources.VersionConflict
exception.
This should work even if called from pip, but...
This doesn't seem to work with build isolation (PEP 517, pyproject.toml
), because in such a case there is usually no pip at all in the build environment.
Reference:
- https://setuptools.readthedocs.io/en/stable/pkg_resources.html?highlight=require#basic-workingset-methods
来源:https://stackoverflow.com/questions/60068313/include-minimum-pip-version-in-setup-py