我试图从一个私有的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

我想要达到的目标可能吗?如果有,我该怎么做?


当前回答

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

然后你可以打字

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

其他回答

只需从原始的git克隆命令(或从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

作为一个额外的技术,如果你有本地克隆的私有存储库,你可以这样做:

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

更现代的,你可以这样做(-e表示你不需要在更改被反映之前提交更改):

pip install -e C:\repo\directory

如果需要在命令行一行程序中执行此操作,也可以这样做。我能够这样做部署在谷歌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

可以使用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+git@gitlab.mycorp.com/my_name/my_repo.git

没有ssh:…这对我很有用。