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

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


当前回答

当我从GitHub安装时,我能够使用:

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

但是,由于我必须作为sudo运行pip, SSH密钥不再与GitHub一起工作,并且“git克隆”在“权限被拒绝(publickey)”上失败。使用git+https允许我以sudo的方式运行命令,并让GitHub询问我的用户名/密码。

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

其他回答

如果不想使用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>

但是,由于我必须作为sudo运行pip, SSH密钥不再与GitHub一起工作,并且“git克隆”在“权限被拒绝(publickey)”上失败。使用git+https允许我以sudo的方式运行命令,并让GitHub询问我的用户名/密码。

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

如果你想在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}。

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

它也适用于Bitbucket:

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

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

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

然后你可以打字

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