我正在尝试从私有 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
我正在努力实现的目标是可能的吗?如果是这样,我该怎么做?
eval $(ssh-agent); ssh-add ~/.ssh/github_id_rsa
,然后在 pip.pypa.io/en/stable/reference/pip_install/#git 之后运行 pip 安装
您可以使用 git+ssh
URI 方案,但您必须设置用户名。注意 URI 中的 git@
部分:
pip install git+ssh://git@github.com/echweb/echweb-utils.git
另请阅读 deploy keys。
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
版本可以是分支、标签或提交哈希
如果从 pip 安装,您需要提供 --process-dependency-links
setup.py
不应包含具体的依赖关系,只能包含抽象的依赖关系。这是指定对 setup.py
的具体依赖。
我想出了一种自动“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 gave us 在 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
该软件包现在应该在没有任何密码提示的情况下安装。
如果您想从 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}
。
这种方法有一个明显的优势。您有一个包含所有依赖项的需求文件。
需求文件的语法在这里给出:
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
如果您只想安装它。
egg
是硬性要求吗?我正在尝试通过 ssh 安装带有部署密钥的私有 python 包,但不断收到 ERROR: Command errored out with exit status 128
的响应。
如果您需要在命令行单行中执行此操作,它也是可能的。我能够在 Google Colab 上进行部署:
创建个人访问令牌:https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token 运行:pip install git+https://
您还可以通过 .netrc
文件为 curl 提供登录凭据(登录名和密码,或部署令牌),通过 git+https://github.com/... URL 安装私有存储库依赖项:
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_A
和 repo_B
的所有者,在 repo_B
、as a Github action 的 python
单元测试期间需要pip install repo_A
。
我为解决此任务而遵循的步骤:
为我的帐户创建了个人访问令牌。至于它的权限,我只需要保留默认权限,即 .ie repo - 完全控制私有存储库。
在 repo_B 下创建了一个存储库机密,将我的个人访问令牌粘贴在那里并将其命名为 PERSONAL_ACCESS_TOKEN。这很重要,因为与 Jamie 提出的解决方案不同,我不需要在 github 操作 .yml 文件中显式公开我宝贵的原始个人访问令牌。
最后,通过 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
actions
(工作流程)pip install
私有存储库时,这可能非常有用,您不想与他人共享 SSH 密钥,即使是作为 Githubsecret
。 Tl; 博士,据我所知,访问令牌的许可较少。
如果不想使用 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>
egg
是硬性要求吗?我正在尝试通过 ssh 安装带有部署密钥的私有 python 包,但不断收到 ERROR: Command errored out with exit status 128
的响应。
如果你在 GitHub、GitLab 等上有自己的库/包,你必须添加一个标签来提交库的具体版本,例如 v2.0,然后你可以安装你的包:
pip install git+ssh://link/name/repo.git@v2.0
这对我有用。其他解决方案对我不起作用。
oxyum's 解决方案对于这个答案是可以的。我只想指出,如果您使用 sudo
安装,则需要小心,因为密钥也必须为 root 存储(例如,/root/.ssh
)。
然后你可以输入
sudo pip install git+ssh://git@github.com/echweb/echweb-utils.git
--user
标志或 virtualenv。
只需从原始 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
你可以试试
pip install git+git@gitlab.mycorp.com/my_name/my_repo.git
没有 ssh:...
。这对我行得通。
git+ssh://git@github.com/echweb/echweb-utils.git
方案。我在这里提出了一个新问题:stackoverflow.com/questions/18883430/…。任何帮助都会很棒。git+ssh://git@github.com/echweb/echweb-utils.git
放入requirements.txt
也可以,这很棒。GIT_SSH_COMMAND="ssh -i ~/.ssh/my-deploy-key"
一起使用的 SSH 密钥