使用pip install -e指定extras_require

123
当从Git仓库安装时,如何使用pip安装extras_require?
我知道在pypi上可以使用“pip install project[extra]”进行安装。
当从Git仓库中安装时,需要使用“pip install -e git+https://github.com/user/project.git#egg=project”。 但我不知道如何将这两个选项结合起来。

https://dev59.com/rV8e5IYBdhLWcg3wHngs - jezrael
1
我的问题不在于简单的依赖关系,而是额外的内容。 - PhilipGarnero
7个回答

191
这应该可以运行,参考示例#6和#7
对于远程仓库:
pip install -e git+https://github.com/user/project.git#egg=project[extra]

这是给本地人的(感谢@Kurt-Bourbaki):
pip install -e .[extra]

根据@Kurt-Bourbaki的说法:
如果你正在使用zsh,你需要转义方括号或使用引号:
pip install -e .\[extra\]
# or
pip install -e ".[extra]"

根据 @Epoc 的说法:
Windows Powershell 也需要引用括号。

正是我所需要的!当你在 requirements.txt 或 setup.py 中定义它时,这也可以工作。 - Joren Van Severen
2
但是如果使用 pip install -e . 而不是 URL 呢? - ankostis
57
"@ankostis pip install -e .[extra] 应该可以运行。如果您使用的是 zsh,则需要转义方括号:pip install -e .\[extra\]。" - Kurt Bourbaki
4
在Windows上,PowerShell会抱怨括号。只需要将参数放在双引号之间,例如:pip install -e ".[extra]" - Epoc
@ankostis的回答对于使用zsh很有帮助,但是像@Epoc建议的引号也可以工作(可能适用于任何shell/platform)。因此,答案应该是: pip install -e ".[extra]" - dyoll
显示剩余4条评论

44

需要注意的是:在括号内或周围不应有空格。例如,这样可以工作:-e ".[extra1,extra2]"这样行不通:-e ". [extra1, extra2]" ——甚至在requirements.txt文件中,这一点也不明显。它最糟糕的问题是当你有空格时,额外内容会被默默忽略。


1
我在多个额外信息方面没有看到过这个记录,但这是一个非常重要的提示! - bastula
@bastula 你说得对,这似乎没有记录——但至少在撰写本文时它是这样运作的。 - MarSoft

16

对于一些用户来说可能不是很明显,对我而言也是如此,因此想要强调以下命令中的extra

pip install -e ".[extra]"

需要用实际的额外要求名称替换 needs to be replaced by the actual name of the extra requirements.

例子:

您可以按照以下方式向您的 setup.cfg 添加 options.extras_require 部分:

[options.extras_require]
  test =
    pre-commit>=2.10.1,<3.0
    pylint>=2.7.2,<3.0
    pytest>=6.2.2,<7.0
    pytest-pspec>=0.0.4,<1.0

然后您按照以下方式安装test附加组件

pip install -e ".[test]"

options.extras_require这一部分对我来说不起作用,导致出现错误。如果我的pyproject.toml文件中有options.extras_require,要么它被忽略了,要么会报错无效的TOML格式,而谷歌搜索只给我提供了关于现在已经不正确的语法的结果。那我该怎么办呢? - Eric Buist

5
这也适用于从whl文件安装的情况,例如,您可以执行以下操作:
pip install path/to/myapp-0.0.1-py3-none-any.whl[extra1]

从文档中很难理解,而且不太直观。


4

如果要从git安装project的额外要求,更现代的语法是:

extra
python -m pip install -e "project[extra] @ git+https://github.com/user/project.git"

来源:在pip文档示例中,查看“安装带有额外功能的软件包”。

通过当前被接受的答案中的命令进行安装——

python -m pip install -e git+https://github.com/user/project.git#egg=project[extra]

—将引发此警告:

DEPRECATION: git+https://github.com/user/project.git#egg=project[extra] 包含一个非 PEP 508 名称的 egg 片段,pip 25.0 将强制执行此行为更改。可能的替代方法是使用 req @ url 语法,并删除 egg 片段。讨论可在 https://github.com/pypa/pip/issues/11617 找到。


0

使用 git + ssh 从私有仓库安装带有额外功能的软件包:

pip install -e 'git+ssh://git@github.com/user/project.git#egg=project[extra1,extra2]'

1
额外的内容之间不应该有空格。此外,使用git安装软件包已经在被接受的答案中涵盖,尽管使用的是https而不是ssh。我认为这个答案最好作为对被接受答案的编辑,旨在累积和结构化知识。 - Ciprian Tomoiagă

0
setup.cfg和requirements.txt的行为不同,在最后有一个解决方法。
# setup.cfg
...
[options.extras_require]
dev =
    pytest
    pytest-cov
    pylint
    coverage
    mypy
    types-requests
    custolint

ciur =
    # ciur==0.2.0
    ciur # 0.2.0

selenium =
    ada-automation.selenium-bot
...

# setup.py

from setuptools import setup

setup()

安装可编辑

pip install \
    -e "${BITBUCKET_ORG_REPOS}/ada-automation/scraping_bot/_lib" \
    -e "${BITBUCKET_ORG_REPOS}/python-ciur" \
    -e ".[ciur,selenium,dev]"


确认这是可编辑的

> pip list | egrep 'ciur|selenium|pytest'
ada-automation.selenium-bot 0.0.0      /Users/xxx/bitbucket.org/ada-automation/scraping_bot/_lib
ciur                        0.2.0      /Users/xxx/bitbucket.org/python-ciur
pytest                      7.4.0
pytest-cov                  4.1.0
selenium                    4.0.0


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接