是否可以使用 pip 从私有 GitHub 存储库安装包?
- 2025-03-06 08:53:00
- admin 原创
- 40
问题描述:
我正在尝试从私有 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
我试图实现的目标有可能实现吗?如果可以,我该怎么做?
解决方案 1:
您可以使用git+ssh
URI 方案,但必须设置用户名。请注意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
解决方案 2:
作为一种附加技术,如果您已经在本地克隆了私有存储库,则可以执行以下操作:
pip install git+file://c:/repo/directory
更现代的做法是,您只需这样做(这-e
意味着您不必在更改反映出来之前提交更改):
pip install -e C:
epodirectory
解决方案 3:
您可以直接使用 HTTPS URL 执行此操作,如下所示:
pip install git+https://github.com/username/repo.git
例如,只需将该行附加到Django项目的 requirements.txt 中即可。
解决方案 4:
它也适用于Bitbucket:
pip install git+ssh://git@bitbucket.org/username/projectname.git
在这种情况下,Pip 将使用您的 SSH 密钥。
解决方案 5:
我发现使用令牌比使用 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 中
您需要在链接末尾包含一些版本号(这里是),即使PyPI
0
上没有任何软件包。这必须是实际的数字,而不是一个单词。您需要先
git+
告诉 setuptools 它要克隆存储库,而不是指向 zip/tarballversion
可以是分支、标签或提交哈希--process-dependency-links
如果从 pip 安装,则需要提供
解决方案 6:
我找到了一种自动“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 为我们提供了使用 pip 和 SSH 的秘诀:
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
解决方案 7:
如果您想从 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-token
和GIT_PASS=${CI_JOB_TOKEN}
。
这种方法有一个明显的优势。你有一个包含所有依赖项的单一需求文件。
解决方案 8:
如果你需要在命令行中执行此操作,这也是可行的。我能够在 Google Colab 上执行此操作进行部署:
创建个人访问令牌:https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
跑步:
pip install git+https://<USERNAME>:<PERSONAL ACCESS TOKEN>@github.com/<ACCOUNT>/<REPOSITORY>.git
解决方案 9:
我的情况比答案中描述的大多数情况都要复杂一些。我是两个私有存储库的所有者repo_A
,并且repo_B
属于 Github 组织,需要在的单元测试pip install repo_A
期间,作为 Github 操作。python
`repo_B`
我解决此任务所遵循的步骤:
为我的账户创建了个人访问令牌。至于它的权限,我只需要保留默认权限,即repo - 完全控制私有存储库。
在 下创建了一个存储库机密
repo_B
,将我的个人访问令牌粘贴到其中并将其命名为PERSONAL_ACCESS_TOKEN
。这很重要,因为与Jamie提出的解决方案不同,我不需要在 github 操作.yml
文件中明确公开我宝贵的原始个人访问令牌。最后,通过 HTTPS(不是
pip install
SSH)从源代码获取包如下:
export PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }}
pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git
解决方案 10:
需求文件的语法如下:
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
如果您只是想要安装它。
解决方案 11:
您还可以通过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"
解决方案 12:
当我从 GitHub 安装时,我可以使用:
pip install git+ssh://git@github.com/<username>/<projectname>.git#egg=<eggname>
但是,由于我必须以 的身份运行 pip sudo
,SSH 密钥不再适用于 GitHub,并且“git clone”因“权限被拒绝(公钥)”而失败。使用git+https
允许我以 sudo 身份运行命令,并让 GitHub 询问我的用户/密码。
sudo pip install git+https://github.com/<username>/<projectname>.git#egg=<eggname>
解决方案 13:
如果您不想使用 SSH,您可以在 HTTPS URL 中添加用户名和密码。
下面的代码假设您在工作目录中有一个名为“pass”的文件,其中包含您的密码。
export PASS=$(cat pass)
pip install git+https://<username>:$PASS@github.com/echweb/echweb-utils.git
解决方案 14:
如果您在 GitHub、GitLab 等网站上有自己的库/包,则必须添加标签以提交该库的具体版本,例如 v2.0,然后才能安装您的包:
pip install git+ssh://link/name/repo.git@v2.0
这对我有用。其他解决方案对我不起作用。
解决方案 15:
git clone
只需从原始命令(或从)复制远程即可git remote -v
。您将获得类似以下内容的内容:
Bitbucket:
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
解决方案 16:
oxyum 的解决方案对于这个答案来说没问题。我只想指出,如果您使用 安装,则需要小心,sudo
因为还必须为 root 存储密钥(例如/root/.ssh
)。
然后你可以输入
sudo pip install git+ssh://git@github.com/echweb/echweb-utils.git
解决方案 17:
你可以尝试
pip install git+git@gitlab.mycorp.com/my_name/my_repo.git
没有ssh:...
。这对我来说有效。