在 setup.py 中包含最低 pip 版本 [英] Include minimum pip version in setup.py

查看:62
本文介绍了在 setup.py 中包含最低 pip 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序创建了一个 setup.py.我在 install_requires 中设置的一些依赖项需要 pip 19.3.1 或更高版本.

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.

有没有办法在 setup.py 中检查 pip 版本?并在构建之前升级 pip?

Is there a way to check for pip version as part of setup.py? and to upgrade pip prior to build?

推荐答案

这不是您在项目中针对其他项目打包中的问题构建变通方法的责任.这是一种不好的做法.无论如何,作为 setup.py 的一部分执行此操作也没有多大意义,因为在许多情况下,此文件在安装时间期间不会执行.

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.

第二好的事情是通知您项目的用户.在您自己项目的文档中清楚说明这个问题以及如何防止它(即安装 pip 版本 19.3.1 或更高版本").

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

更新:

如果您决定在 setup.py 中强制执行检查,这里有一些技术可能会有所帮助...

If you decide to enforce a check in setup.py anyway, here are some techniques that might help...

但我仍然建议不要使用这些,因为您的 setup.py 实际上并没有错,但问题似乎在于依赖项的打包.

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(
    # ...
)

这会触发异常:

pkg_resources.DistributionNotFound: The 'pip>=19.3.1' distribution was not found and is required by the application

这种技术的缺点是它不会在从 pip 调用时触发(例如:pip install .),因为在这种情况下 __main__ 模块不是 setup.py 而是 pip 的一个模块.

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.

参考:

2.

import pkg_resources
import setuptools

pkg_resources.require(['pip >= 19.3.1'])

setuptools.setup(
    # ...
)

这会触发 pkg_resources.VersionConflict 异常.

即使从 pip 调用这也应该可以工作,但是...

This should work even if called from pip, but...

这似乎不适用于构建隔离(PEP 517pyproject.toml),因为在这种情况下通常在构建环境中根本没有 pip.

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.

参考:

这篇关于在 setup.py 中包含最低 pip 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆