Python setuptools在安装时排除依赖项。

huangapple go评论49阅读模式
英文:

Python setuptools exclude dependencies when installing

问题

Python的setuptools允许您指定可选依赖项,但它是否允许您执行相反的操作?

例如,假设我在my_package中有一个依赖项列表,如下所示:

  • numpy
  • pandas

因此,如果我使用pip install my_package安装包,它还将安装这两个依赖项。

然而,对于某些用例,用户可能不需要pandas。因此,我想要执行类似pip install my_package[~pandas]之类的操作,指示pip不安装pandas。

目前是否支持这样的操作?

英文:

Python setuptools allows you to specify optional dependencies, but does it allow you to do something in the inverse?

For example, let's say I have a list of dependencies in my_package like below:

  • numpy
  • pandas

So if I installed the package with pip install my_package, it would also install these two dependencies.

However, for certain use cases, a user may not need pandas. So I would want to do something like pip install my_package[~pandas] or something like that to instruct pip to not install pandas.

Is this something that is currently supported?

答案1

得分: 2

目前不支持此功能 - 额外内容严格是累加的。已经提出了多次建议,但似乎从未有过进展。

最新的讨论在这里:

提案 - 扩展可选依赖项以支持取消建议/默认可安装项的选择

作为一种解决方法,您可以使用:

pip install --no-deps my_package

但这将排除_每一个_依赖项,包括pandas。您需要手动查找并安装其他依赖项。

英文:

It is not currently supported - extras are strictly additive. It has been proposed several times, but the discussions never seem to get anywhere.

The latest discussion is here:

Proposal - expanding optional dependencies to support opt-out of recommended/default installables.

As a workaround, you can use:

pip install --no-deps my_package

But this would exclude every dependency, including pandas. You'd have to find and install the other dependencies manually.

答案2

得分: 1

正如您提到的,虽然可以通过以下方式安装可选依赖项:

pip install my_package[super,cool]

但是相反的情况,阻止安装依赖项 (!cool) 是不可能的。Wim已经提到了 --no-deps 的解决方法。另一个解决方法可能是提供两个版本,my_packagemy_package_vanilla,它们具有不同的依赖关系。然后用户可以按照以下方式安装:

  • 使用 my_package 安装普通用户所需的所有默认依赖项
  • 使用 my_package_vanilla 仅安装 my_package 的最基本版本(可能甚至没有附加功能?)
  • 使用 my_package_vanilla[pandas,super,cool] 安装用户选择的三组额外依赖项。
英文:

As you mentioned, while it is possible to install optional dependencies with, for example

pip install my_package[super,cool]

the opposite, keeping from installing dependencies (!cool) is not possible. Wim already mentioned the --no-deps workaround. Another workaround might be to provide two versions, my_package and my_package_vanilla with different dependencies. Then user could install, for example:

  • my_package to install all default dependencies needed by the average user
  • my_package_vanilla to install just the bare bones version of my_package (perhaps even non-functional without extras?)
  • my_package_vanilla[pandas,super,cool] installing three sets of extra dependencies chosen by the user.

huangapple
  • 本文由 发表于 2023年6月1日 03:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定