You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过pip查看Python包extras_require的可选依赖选项?

Great question! Yes, pip does offer ways for users to discover the optional extras your package provides, plus there are other straightforward methods to make this clear. Let's break it down:

For packages hosted on PyPI

If your package is published to PyPI, users can run this command to view details including available extras:

pip index show Foo

The output will include a section listing optional dependencies tied to each extra (look for lines like barpy; extra == 'bar'), and often explicitly calls out all valid extra names too.

For local source packages

If someone is working with your code locally (before installing it), they have a couple easy options:

  • Check your setup.py or pyproject.toml directly: This is the simplest route. They can open your setup file and look at the extras_require dictionary to see all available extras and their associated dependencies.
  • Use an intentional "invalid extra" error: If they try installing a non-existent extra (like pip3 install .[fake-extra]), pip will throw an error message that lists all valid extras—something like "Invalid extra(s): 'fake-extra'. Valid extras are: bar". It's a quick trick to reveal options without digging into code.

Pro tip for package authors

While pip can show extras, don't rely on that alone! Most users won't think to run those commands. Make sure to document your extras clearly in your README, documentation, or even your package's PyPI description. For example, add a line like:

Want to use the bar feature? Install with optional dependencies: pip3 install Foo[bar]
This makes it obvious for users who might not even know extras exist.


内容的提问来源于stack exchange,提问作者quazgar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:23:53