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?
As the setup.py file is installed via pip (and pip itself is run by the python interpreter) it is not possible to specify which Python version to use in the setup.py file.
pip is a package manager, which can install, upgrade, list and uninstall packages, like familiar package managers including: dpkg, apt, yum, urpmi, ports etc. Under the hood, it will run python setup.py install , but with specific options to control how and where things end up installed. In summary: use pip .
How do I Install a Specific Version of a Python Package? To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion . For example, if you want to install an older version of Pandas you can do as follows: pip install pandas==1.1. 3 .
As a first step, pip needs to get metadata about a package (name, version, dependencies, and more). It collects this by calling setup.py egg_info . The egg_info command generates the metadata for the package, which pip can then consume and proceed to gather all the dependencies of the package.
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:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With