使用pip安装特定的git提交版本

278
4个回答

473

您可以指定提交哈希值、分支名称或标签。

对于分支名称和标签,您还可以安装压缩的分发版本。这样更快、更高效,因为它不需要克隆整个存储库。GitHub会自动创建这些捆绑包。

哈希:

$ pip install git+https://github.com/aladagemre/django-notification.git@2927346f4c513a217ac8ad076e494dd1adbf70e1

分支名称

使用Git

$ pip install git+https://github.com/aladagemre/django-notification.git@cool-feature-branch

或者从源代码包中获取

$ pip install https://github.com/aladagemre/django-notification/archive/cool-feature-branch.tar.gz

标签

使用git

$ pip install git+https://github.com/aladagemre/django-notification.git@v2.1.0

或者从源码包中获取

$ pip install https://github.com/aladagemre/django-notification/archive/v2.1.0.tar.gz

这是一个文档记录不够完备的功能,但您可以在https://pip.pypa.io/en/latest/topics/vcs-support/找到更多信息。


31
它将添加这条有趣的信息:找不到标签或分支'2927346f4c513a217ac8ad076e494dd1adbf70e1',假设为提交。 - vlad-ardelean
13
@vlad-ardelean 你有什么办法告诉pip这是一个提交吗?这个输出在我的部署脚本中,我不想压制所有的stderr。 - Leonardo Arroyo
1
@ScottStafford 你不应该有一个分支/标签,它的名称与提交消息相同。那会...很奇怪。 - Martin Thoma
3
另外我想提一下一个HTTPS版本的 git+ 命令:pip install git+https://github.com/gpoore/codebraid@011464539bfb09b8611c8aef0d543532cea958bf。这对于身处公司http代理后面的人来说可能很重要。 - Grwlf
1
本问题的git+git://...版本从今天开始已不再适用于GitHub。请参考https://github.blog/2021-09-01-improving-git-protocol-security-github/。 - Marco Poli
显示剩余3条评论

32

你可以通过在项目的requirements.txt文件中添加以下行来自动安装Python包:

package-name -e git+https://github.com/owner/repository.git@branch_or_commit#egg={package-name}

然后运行以下命令:

$ pip install -r requirements.txt


19
对于我来说(在Python3.5虚拟环境中使用pip 9.0.1),执行 pip install -r requirements.txt 时出现了错误:'Could not detect requirement name, please specify one with #egg='。但是,使用格式 '-e git+https://github.com/owner/repository.git#egg=branch_or_commit' 可以正常工作。 - Edouard Berthe
你需要在 requirements.txt 文件中使用这个格式。你做到了吗? - mannysz
2
我已经让它工作了,但是这不太清楚。需要在行首加上“package_name -e ...”,而不仅仅是“-e ...”。 - Udi
一个在我的 requirements.txt 文件中的行的工作示例:python-openid -e git+https://github.com/openid/python-openid.git@d093a0919198eb53826ae5753e517af10ad95d5b#egg={python-openid} - spodell
已添加建议。谢谢。 - mannysz
1
package -e url 语法对我来说不起作用。我猜这是表示可编辑构建,但是当我尝试从 Github 安装时它并没有起作用。只需像接受的答案中一样指定实际的 git+https://github.com/... URL,在 Python 3.8.2 上使用 pip 19.2.3 对我有效。 - tripleee

25

对于 @hugo-tavares 的回答,我有一个额外的评论:

如果这是一个私有的 GitHub 代码库,你需要使用以下命令:

pip install git+ssh://git@github.com/....

就您的情况而言:

pip install git+ssh://git@github.com/aladagemre/django-notification.git@2927346f4c513a217ac8ad076e494dd1adbf70e1

4
如果你想创建一个egg包,你仍然可以使用相同的@branch_or_commit附加项:pip install git+ssh://git@github.com/myrepo.git@mybranch#egg=myeggscript

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