防止将软件包安装在旧的Python版本上 [英] Prevent package from being installed on old Python versions

查看:126
本文介绍了防止将软件包安装在旧的Python版本上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用不受支持的Python版本时,我们可以放入setup.py文件中以防止pip收集和尝试安装软件包吗?

What can we put in a setup.py file to prevent pip from collecting and attempting to install a package when using an unsupported Python version?

例如 magicstack 是一个用trove分类器列出的项目:

For example magicstack is a project listed with the trove classifier:

Programming Language :: Python :: 3 :: Only

因此,如果pip --version与python 2.7绑定,我期望出现以下行为:

So I expect the following behaviour if pip --version is tied to python 2.7:

$ pip install magicstack
Collecting magicstack
  Could not find a version that satisfies the requirement magicstack (from versions: )
No matching distribution found for magicstack

但是实际行为是pip收集了一个发行版,下载了该发行版,尝试安装它,然后失败了.还有其他仅适用于Python3的版本,例如 curio ,实际上可以很好地安装-因为不使用任何特定于Python 3的内容-只是在使用某些仅适用于Python 3的语法时在导入时失败.而且我确定有些软件包可以安装OK,可以导入OK,并且可能仅在运行时失败!

But the actual behavior is that pip collects a release, downloads it, attempts to install it, and fails. There are other Python3-only releases, curio for example, which actually install fine - because the setup.py didn't use anything Python 3 specific - only to fail at import time when some Python 3 only syntax is used. And I'm sure there are packages which install OK, import OK, and maybe only fail at runtime!

以pip会尊重的方式指定受支持的Python版本的正确方法是什么?我找到了一种解决方法,仅上传wheel文件,并拒绝上传.tar .gz发行版,但我很想知道正确的解决方法.

What is the correct method to specify your supported Python versions in a way that pip will respect? I've found a workaround, involving uploading only a wheel file, and refusing to uploading a .tar.gz distribution, but I would be interested to know the correct fix.

如果Python/os/体系结构不匹配,pip如何知道下载车轮分布?它只是使用 .whl文件名约定还是有比幕后发生的事情更复杂的事情吗?我们能否以某种方式将元数据提供给源代码分发,以使pip使用.tar.gz上传文件做正确的事情?

How does pip know not to download the wheel distribution if the Python/os/architecture is not matching? Does it just use the .whl filename convention or is there something more sophisticated than that happening behind the scenes? Can we somehow give the metadata to a source distribution to make pip do the right thing with .tar.gz uploads?

推荐答案

有一种正确的方法,但是遗憾的是pip仅在版本9.0.0(2016-11-02发布)中开始支持它.使用较旧版本的pip的用户将继续下载willy-nilly软件包,无论它们使用的是哪个Python版本.

There is a correct way to do this, but unfortunately pip only started supporting it in version 9.0.0 (released 2016-11-02), and so users with older versions of pip will continue to download packages willy-nilly regardless of what Python version they're for.

setup.py文件中,向setup()传递python_requires参数,该参数以版本说明符.例如,如果您的软件包仅适用于Python 3+,请输入:

In your setup.py file, pass setup() a python_requires argument that lists your package's supported Python versions as a PEP 440 version specifier. For example, if your package is for Python 3+ only, write:

setup(
    ...
    python_requires='>=3',
    ...
)

如果您的软件包适用于Python 3.3及更高版本,但您仍不愿意支持Python 4,请输入:

If your package is for Python 3.3 and up but you're not willing to commit to Python 4 support yet, write:

setup(
    ...
    python_requires='~=3.3',
    ...
)

如果您的软件包适用于Python 2.6、2.7和所有版本从3.3开始的Python 3,请输入:

If your package is for Python 2.6, 2.7, and all versions of Python 3 starting with 3.3, write:

setup(
    ...
    python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4',
    ...
)

以此类推.

完成此操作后,您需要将setuptools的版本至少升级到24.2.0,以便处理python_requires参数.早期版本只会在警告时将其忽略.然后,您项目中所有构建的sdists和wheels都将包含相关的元数据,这些元数据将告诉PyPI告诉pip它们适用于什么Python版本.

Once you've done that, you will need to upgrade your version of setuptools to at least 24.2.0 in order for the python_requires argument to be processed; earlier versions will just ignore it with a warning. All of your project's sdists and wheels built afterwards will then contain the relevant metadata that tells PyPI to tell pip what Python versions they're for.

这篇关于防止将软件包安装在旧的Python版本上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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