Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include minimum pip version in setup.py

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?

like image 919
JLuxton Avatar asked Feb 05 '20 02:02

JLuxton


People also ask

How do I set Python version in setup py?

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.

Does pip install run setup py?

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 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 .

Does pip need setup py?

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.


1 Answers

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
like image 50
sinoroc Avatar answered Oct 19 '22 16:10

sinoroc