pip 需求文件中的可选依赖项

发布于 2024-09-18 03:54:31 字数 156 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨的味道风的声音 2024-09-25 03:54:31

2015 年 PEP-0508 定义了一种在 < 中指定可选依赖项的方法code>requirements.txt:

requests[security]

这意味着 yourpackage 需要 requests 来实现其安全选项。您可以将其安装为:

pip install yourpackage[security]

In 2015 PEP-0508 defined a way to specify optional dependencies in requirements.txt:

requests[security]

That means that yourpackage needs requests for its security option. You can install it as:

pip install yourpackage[security]
灯下孤影 2024-09-25 03:54:31

您可以创建一个 optional-requirements.txt 和一个 requirements.txt,而不是在与硬性要求相同的文件中指定可选依赖项。

要将当前环境的包导出到文本文件中,您可以执行以下操作:

pip freeze > requirements.txt

如有必要,修改requirements.txt 的内容以准确表示项目的依赖项。然后,要安装此文件中的所有软件包,请运行:

pip install -U -r 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 a requirements.txt.

To export your current environment's packages into a text file, you can do this:

pip freeze > requirements.txt

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:

pip install -U -r requirements.txt

-U tells pip to upgrade packages to the latest version, and -r tells it to install all packages in requirements.txt.

她如夕阳 2024-09-25 03:54:31

您误解了文档;情况并不那么清楚。文档中的要点是,通过需求文件,您可以随意指定完整推荐的包工作集,包括必要的依赖项和可选的依赖项。

您可以添加注释(以 # 开头的行)来区分两者,但 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文