461

我正在尝试从私有 GitHub 存储库安装 Python 包。对于公共存储库,我可以发出以下运行良好的命令:

pip install git+git://github.com/django/django.git

但是,如果我尝试将其用于私有存储库:

pip install git+git://github.com/echweb/echweb-utils.git

我得到以下输出:

Downloading/unpacking git+git://github.com/echweb/echweb-utils.git
Cloning Git repository git://github.com/echweb/echweb-utils.git to /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build
Complete output from command /usr/local/bin/git clone git://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build:
fatal: The remote end hung up unexpectedly

Cloning into /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build...

----------------------------------------
Command /usr/local/bin/git clone git://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-VRsIoo-build failed with error code 128

我想这是因为我试图在不提供任何身份验证的情况下访问私有存储库。因此,我尝试使用 Git +ssh希望 pip 使用我的 SSH 公钥进行身份验证:

pip install git+ssh://github.com/echweb/echweb-utils.git

这给出了以下输出:

Downloading/unpacking git+ssh://github.com/echweb/echweb-utils.git
Cloning Git repository ssh://github.com/echweb/echweb-utils.git to /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build
Complete output from command /usr/local/bin/git clone ssh://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build:
Cloning into /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build...

Permission denied (publickey).

fatal: The remote end hung up unexpectedly

----------------------------------------
Command /usr/local/bin/git clone ssh://github.com/echweb/echweb-utils.git /var/folders/cB/cB85g9P7HM4jcPn7nrvWRU+++TI/-Tmp-/pip-DQB8s4-build failed with error code 128

我正在努力实现的目标是可能的吗?如果是这样,我该怎么做?

4

17 回答 17

502

您可以使用git+sshURI 方案,但必须设置用户名。注意git@URI 中的部分:

pip install git+ssh://git@github.com/echweb/echweb-utils.git

另请阅读部署密钥

PS:在我的安装中,“git+ssh”URI 方案仅适用于“可编辑”要求:

pip install -e URI#egg=EggName

请记住:在命令中使用遥控器地址之前,将打印的字符更改为:字符:git remote -v/pip

$ git remote -v
origin  git@github.com:echweb/echweb-utils.git (fetch)
#                     ^ change this to a '/' character

如果你忘记了,你会得到这个错误:

ssh: Could not resolve hostname github.com:echweb:
         nodename nor servname provided, or not known
于 2011-01-29T15:08:11.407 回答
87

作为一项附加技术,如果您在本地克隆了私有存储库,您可以执行以下操作:

pip install git+file://c:/repo/directory

更现代的,你可以这样做(这-e意味着你不必在它们被反映之前提交更改):

pip install -e C:\repo\directory
于 2013-01-21T02:17:50.887 回答
53

您可以像这样直接使用 HTTPS URL 进行操作:

pip install git+https://github.com/username/repo.git

例如,这也适用于在Django项目的 requirements.txt 中附加该行。

于 2013-03-24T11:19:28.737 回答
38

它也适用于Bitbucket

pip install git+ssh://git@bitbucket.org/username/projectname.git

在这种情况下,Pip 将使用您的 SSH 密钥。

于 2012-09-21T02:09:58.243 回答
29

我发现使用令牌比使用 SSH 密钥容易得多。我在这方面找不到太多好的文档,所以我主要通过反复试验找到了这个解决方案。此外,从 pip 和 setuptools 安装有一些细微的差别;但这种方式应该适用于两者。

GitHub(目前,截至 2016 年 8 月)不提供获取私有存储库的 zip/tarball 的简单方法。所以你需要指向 setuptools 来告诉 setuptools 你指向的是一个 Git 仓库:

from setuptools import setup
import os
# Get the deploy key from https://help.github.com/articles/git-automation-with-oauth-tokens/
github_token = os.environ['GITHUB_TOKEN']

setup(
    # ...
    install_requires='package',
    dependency_links = [
    'git+https://{github_token}@github.com/user/{package}.git/@{version}#egg={package}-0'
        .format(github_token=github_token, package=package, version=master)
        ]

这里有几点注意事项:

  • 对于私有仓库,需要通过 GitHub 进行身份验证;我发现最简单的方法是创建一个OAuth令牌,将其放入您的环境中,然后将其包含在 URL 中
  • 您需要在链接末尾包含一些版本号(这里是0),即使PyPI上没有任何包。这必须是一个实际的数字,而不是一个字。
  • 您需要先git+告诉 setuptools 它是克隆存储库,而不是指向 zip / tarball
  • version可以是分支、标签或提交哈希
  • --process-dependency-links如果从 pip 安装,您需要提供
于 2016-08-31T16:43:28.413 回答
22

我想出了一种自动“pip install”一个不需要密码提示的 GitLab 私有存储库的方法。这种方法使用 GitLab“部署密钥”和 SSH 配置文件,因此您可以使用个人 SSH 密钥以外的密钥进行部署(在我的情况下,供“机器人”使用)。也许有好心人可以使用 GitHub 进行验证。

创建一个新的 SSH 密钥:

ssh-keygen -t rsa -C "GitLab_Robot_Deploy_Key"

该文件应显示为~/.ssh/GitLab_Robot_Deploy_Key~/.ssh/GitLab_Robot_Deploy_Key.pub

将文件的内容复制并粘贴~/.ssh/GitLab_Robot_Deploy_Key.pub到 GitLab“部署密钥”对话框中。

测试新的部署密钥

以下命令告诉 SSH 使用您的新部署密钥来设置连接。成功后,您应该会收到消息:“欢迎来到 GitLab,用户名!”

ssh -T -i ~/.ssh/GitLab_Robot_Deploy_Key git@gitlab.mycorp.com

创建 SSH 配置文件

接下来,使用编辑器创建~/.ssh/config文件。添加以下内容。'Host' 值可以是您想要的任何值(记住它,因为稍后您将使用它)。HostName 是 GitLab 实例的 URL。identifyFile 是您在第一步中创建的 SSH 密钥文件的路径。

Host GitLab
  HostName gitlab.mycorp.com
  IdentityFile ~/.ssh/GitLab_Robot_Deploy_Key

将 SSH 指向配置文件

oxyum 为我们提供了在 SSH 中使用 pip 的秘诀:

pip install git+ssh://git@gitlab.mycorp.com/my_name/my_repo.git

我们只需要稍微修改一下,让 SSH 使用我们的新部署密钥。我们通过将 SSH 指向 SSH 配置文件中的主机条目来做到这一点。只需将命令中的 'gitlab.mycorp.com' 替换为我们在 SSH 配置文件中使用的主机名即可:

pip install git+ssh://git@GitLab/my_name/my_repo.git

该软件包现在应该在没有任何密码提示的情况下安装。

参考 A
参考 B

于 2017-05-02T23:31:00.127 回答
16

如果你想从CI服务器或类似的需求文件中安装依赖项,你可以这样做:

git config --global credential.helper 'cache'
echo "protocol=https
host=example.com
username=${GIT_USER}
password=${GIT_PASS}
" | git credential approve
pip install -r requirements.txt

就我而言,我使用了GIT_USER=gitlab-ci-tokenand GIT_PASS=${CI_JOB_TOKEN}

这种方法有一个明显的优势。您有一个包含所有依赖项的需求文件。

于 2019-04-04T13:23:27.353 回答
15

需求文件的语法在这里给出:

https://pip.pypa.io/en/latest/reference/pip_install.html#requirements-file-format

例如,使用:

-e git+http://github.com/rwillmer/django-behave#egg=django-behave

如果您希望源在安装后保留。

要不就

git+http://github.com/rwillmer/django-behave#egg=django-behave

如果您只想安装它。

于 2012-09-25T11:20:23.097 回答
11

如果您需要在命令行单行中执行此操作,它也是可能的。我能够在 Google Colab 上进行部署:

  1. 创建个人访问令牌:https ://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
  2. 跑:pip install git+https://<PERSONAL ACCESS TOKEN>@github.com/<USERNAME>/<REPOSITORY>.git
于 2020-08-14T16:27:37.947 回答
10

您还可以通过git+https://github.com/... URL 安装私有存储库依赖项,方法是为curl文件提供登录凭据(登录名和密码,或部署令牌).netrc

echo "machine github.com login ei-grad password mypasswordshouldbehere" > ~/.netrc
pip install "git+https://github.com/ei-grad/my_private_repo.git#egg=my_private_repo"
于 2016-09-15T20:09:52.623 回答
8

如果不想使用 SSH,可以在 HTTPS URL 中添加用户名和密码。

下面的代码假定您在工作目录中有一个名为“pass”的文件,其中包含您的密码。

export PASS=$(cat pass)
pip install git+https://<username>:$PASS@github.com/echweb/echweb-utils.git
于 2018-07-19T18:20:59.593 回答
7

当我从 GitHub 安装时,我可以使用:

pip install git+ssh://git@github.com/<username>/<projectname>.git#egg=<eggname>

但是,由于我必须运行 pip as sudo,SSH 密钥不再与 GitHub 一起使用,并且“git clone”在“Permission denied (publickey)”上失败。使用git+https允许我以 sudo 身份运行命令,并让 GitHub 向我询问我的用户/密码。

sudo pip install git+https://github.com/<username>/<projectname>.git#egg=<eggname>
于 2013-12-02T23:25:57.337 回答
7

我的情况比答案中描述的大多数情况都复杂。我是两个私人存储库repo_A和一个 Github 组织的所有者,repo_B并且需要在单元测试pip install repo_A期间作为 Githubpython操作。repo_B

我为解决此任务而遵循的步骤:

  • 为我的帐户创建了个人访问令牌。至于它的权限,我只需要保留默认的, .ie repo - Full control of private repositories
  • 在下面创建了一个存储库机密repo_B,将我的个人访问令牌粘贴在那里并命名它PERSONAL_ACCESS_TOKEN这很重要,因为与Jamie提出的解决方案不同,我不需要在 github 操作.yml文件中显式公开我宝贵的原始个人访问令牌。
  • 最后,pip install通过 HTTPS(不是SSH)从源代码包如下:
export PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }}

pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git
于 2020-12-25T02:04:20.033 回答
3

如果你在 GitHub、GitLab 等上有自己的库/包,你必须添加一个标签来提交库的具体版本,例如 v2.0,然后你可以安装你的包:

pip install git+ssh://link/name/repo.git@v2.0

这对我有用。其他解决方案对我不起作用。

于 2018-05-24T09:45:47.693 回答
0

oxyum 的解决方案对于这个答案是可以的。我只想指出,如果您正在使用安装,则需要小心,sudo因为密钥也必须为 root 存储(例如,/root/.ssh)。

然后你可以输入

sudo pip install git+ssh://git@github.com/echweb/echweb-utils.git
于 2015-01-12T11:46:48.733 回答
0

只需从原始git clone命令(或从git remote -v)复制遥控器。你会得到这样的东西:

  • 比特桶: git+ssh://git@bitbucket.org:your_account/my_pro.git

  • GitHub: git+ssh://git@github.com:your_account/my_pro.git

接下来,您需要替换:域名/旁边的。

所以安装使用:

pip install git+ssh://git@bitbucket.org/your_account/my_pro.git
于 2019-11-04T08:31:37.310 回答
-1

你可以试试

pip install git+git@gitlab.mycorp.com/my_name/my_repo.git

没有ssh:...。这对我行得通。

于 2018-03-05T07:55:04.220 回答