繁体   English   中英

是否可以使用 pip 从私有 GitHub 存储库安装包?

[英]Is it possible to use pip to install a package from a private GitHub repository?

我正在尝试从私有 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

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

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

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

另请阅读部署密钥

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

pip install -e URI#egg=EggName

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

$ 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

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

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

更现代的是,您可以这样做(并且-e意味着您不必在更改反映之前提交更改):

pip install -e C:\repo\directory

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

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

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

它也适用于Bitbucket

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

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

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

GitHub(目前,截至 2016 年 8 月)不提供获取私有存储库的 zip/tarball 的简单方法。 所以你需要告诉 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可以是分支、标签或提交哈希
  • 如果从 pip 安装,您需要提供--process-dependency-links

我想出了一种自动“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

如果你想从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-tokenGIT_PASS=${CI_JOB_TOKEN}

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

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

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

如果您只想安装它。

如果您需要在命令行单行中执行此操作,它也是可能的。 我能够在 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

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

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"

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

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

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

pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git

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

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

export PASS=$(cat pass)
pip install git+https://<username>:$PASS@github.com/echweb/echweb-utils.git

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

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

但是,由于我必须将 pip 作为sudo运行,SSH 密钥不再适用于 GitHub,并且“git clone”在“Permission denied (publickey)”上失败。 使用git+https可以让我以 sudo 身份运行命令,并让 GitHub 询问我的用户名/密码。

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

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

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

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

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

然后你可以输入

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

只需从原始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

我正在尝试从私有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

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

你可以试试

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM