pip 需求文件中的可选依赖项
如何在 pip 需求文件中指定可选依赖项?
根据 pip 文档 这是可能的,但文档没有解释如何做到这一点,我在网上找不到任何例子。
How can I specify optional dependencies in a pip requirements file?
According to the pip documentation this is possible, but the documentation doesn't explain how to do it, and I can't find any examples on the web.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
2015 年 PEP-0508 定义了一种在 < 中指定可选依赖项的方法code>requirements.txt:
这意味着
yourpackage
需要requests
来实现其安全选项。您可以将其安装为:In 2015 PEP-0508 defined a way to specify optional dependencies in
requirements.txt
:That means that
yourpackage
needsrequests
for its security option. You can install it as:您可以创建一个
optional-requirements.txt
和一个requirements.txt
,而不是在与硬性要求相同的文件中指定可选依赖项。要将当前环境的包导出到文本文件中,您可以执行以下操作:
如有必要,修改requirements.txt 的内容以准确表示项目的依赖项。然后,要安装此文件中的所有软件包,请运行:
-U
告诉pip
将软件包升级到最新版本,-r
告诉它安装requirements.txt中的所有软件包。Instead of specifying optional dependencies in the same file as the hard requirements, you can create a
optional-requirements.txt
and arequirements.txt
.To export your current environment's packages into a text file, you can do this:
If necessary, modify the contents of the requirements.txt to accurately represent your project's dependencies. Then, to install all the packages in this file, run:
-U
tellspip
to upgrade packages to the latest version, and-r
tells it to install all packages in requirements.txt.您误解了文档;情况并不那么清楚。文档中的要点是,通过需求文件,您可以随意指定完整推荐的包工作集,包括必要的依赖项和可选的依赖项。
您可以添加注释(以 # 开头的行)来区分两者,但 pip 没有区别。正如丹尼尔所建议的,您还可以有两个需求文件。
You are misunderstanding the documentation; it's not as clear as it could be. The point in the documentation is that with a requirements file you can feel free to specify your full recommended working set of packages, including both necessary dependencies and optional ones.
You can add comments (lines beginning with #) to distinguish the two to humans, but pip makes no distinction. You can also have two requirements files, as Daniel suggests.