10

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 Answer 1

5

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:

Sign up to request clarification or add additional context in comments.

8 Comments

My old version of pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.7) was pulling dependencies from pypi due to identical private package name even if I had remote GitHub location explicitly specified. After wasting my morning to debug the installation issue, I noticed that the latest pip downloads the package from external link nicely without any weird side effects. I don't see how it would be an bad practice to fail early to a known pip issue.
Not sure I understood the whole comment. -- On failing early: yes, I guess it's acceptable in setup.py to somehow check pip's version number and fail if it's not in the expected range (maybe with __requires__ = ['pip >= 19.3.1']; import setuptools at the top of setup.py). The question also asked about upgrading pip from within setup.py which would be highly unlikely to work reliably.
Agreed, auto installation is bad.
I tried adding __requires__ = ['pip >= 21.3.1'] on top of my setup.py but at least in my case it did not fail.
@Jaakko I added another technique to my answer that should trigger a failure even if called from pip.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.