问题:是否可以使用pip从私有GitHub存储库安装软件包?

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

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

I am trying to install a Python package from a private GitHub repository. For a public repository, I can issue the following command which works fine:

pip install git+git://github.com/django/django.git

However, if I try this for a private repository:

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

I get the following output:

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

I guess this is because I am trying to access a private repository without providing any authentication. I therefore tried to use Git + ssh hoping that pip would use my SSH public key to authenticate:

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

This gives the following output:

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

Is what I am trying to achieve even possible? If so, how can I do it?


回答 0

您可以使用git+sshURI方案,但是必须设置用户名:

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

git@在URI中看到该部分了吗?

PS:另请参阅有关部署密钥

PPS:在我的安装中,“ 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

You can use the git+ssh URI scheme, but you must set a username:

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

Do you see the git@ part into the URI?

PS: Also read about deploy keys.

PPS: In my installation, the “git+ssh” URI scheme works only with “editable” requirements:

pip install -e URI#egg=EggName

Remember: Change the : character that git remote -v prints to a / character before using the remote’s address in the pip command:

$ git remote -v
origin  git@github.com:echweb/echweb-utils.git (fetch)
                      ^ change this to a '/' character

If you forget, you will get this error:

ssh: Could not resolve hostname github.com:echweb:
         nodename nor servname provided, or not known

回答 1

作为另一项技术,如果您在本地克隆了专用存储库,则可以执行以下操作:

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

更现代的是,您可以执行此操作(这-e将意味着您不必在更改被反映之前就提交更改):

pip install -e C:\repo\directory

As an additional technique, if you have the private repository cloned locally, you can do:

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

More modernly, you can just do this (and the -e will mean you don’t have to commit changes before they’re reflected):

pip install -e C:\repo\directory

回答 2

您可以使用HTTPS URL直接执行此操作,如下所示:

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

例如,这也可以仅将这一行添加到Django项目中的requirements.txt中。

You can do it directly with the HTTPS URL like this:

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

This also works just appending that line in the requirements.txt in a Django project, for instance.


回答 3

它也可以与Bitbucket一起使用

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

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

It also works with Bitbucket:

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

Pip will use your SSH keys in this case.


回答 4

需求文件的语法在此处给出:

https://pip.pypa.io/zh_CN/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

如果您只想安装它。

The syntax for the requirements file is given here:

https://pip.pypa.io/en/latest/reference/pip_install.html#requirements-file-format

So for example, use:

-e git+http://github.com/rwillmer/django-behave#egg=django-behave

if you want the source to stick around after installation.

Or just

git+http://github.com/rwillmer/django-behave#egg=django-behave

if you just want it to be installed.


回答 5

我发现使用令牌比使用SSH密钥容易得多。我在这方面找不到很多好的文档,因此我主要是通过反复试验来遇到此解决方案。此外,从pip和setuptools安装有一些细微的差异。但是这种方式对两者都适用。

GitHub尚未提供(目前,截至2016年8月)提供了一种获取私有存储库zip / tarball的简便方法。因此,您需要指向setuptools来告诉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 / tarball
  • version 可以是分支,标签或提交哈希
  • --process-dependency-links如果从pip安装,则需要提供

I found it much easier to use tokens than SSH keys. I couldn’t find much good documentation on this, so I came across this solution mainly through trial and error. Further, installing from pip and setuptools have some subtle differences; but this way should work for both.

GitHub don’t (currently, as of August 2016) offer an easy way to get the zip / tarball of private repositories. So you need to point setuptools to tell setuptools that you’re pointing to a Git repository:

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)
        ]

A couple of notes here:

  • For private repositories, you need to authenticate with GitHub; the simplest way I found is to create an OAuth token, drop that into your environment, and then include it with the URL
  • You need to include some version number (here is 0) at the end of the link, even if there’s isn’t any package on PyPI. This has to be a actual number, not a word.
  • You need to preface with git+ to tell setuptools it’s to clone the repository, rather than pointing at a zip / tarball
  • version can be a branch, a tag, or a commit hash
  • You need to supply --process-dependency-links if installing from pip

回答 6

我想出了一种自动“点安装”不需要密码提示的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文件。添加以下内容。“主机”值可以是您想要的任何值(请记住它,因为稍后会使用它)。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使用我们的新Deploy Key。为此,我们将SSH指向SSH配置文件中的Host条目。只需将命令中的“ gitlab.mycorp.com”替换为我们在SSH配置文件中使用的主机名即可:

pip install git+ssh://git@GitLab/my_name/my_repo.git

该软件包现在应该安装,没有任何密码提示。

参考文献A
参考文献B

I figured out a way to automagically ‘pip install’ a GitLab private repository that requires no password prompt. This approach uses GitLab “Deploy Keys” and an SSH configuration file, so you can deploy using keys other than your personal SSH keys (in my case, for use by a ‘bot). Perhaps someone kind soul can verify using GitHub.

Create a New SSH key:

ssh-keygen -t rsa -C "GitLab_Robot_Deploy_Key"

The file should show up as ~/.ssh/GitLab_Robot_Deploy_Key and ~/.ssh/GitLab_Robot_Deploy_Key.pub.

Copy and paste the contents of the ~/.ssh/GitLab_Robot_Deploy_Key.pub file into the GitLab “Deploy Keys” dialog.

Test the New Deploy Key

The following command tells SSH to use your new deploy key to set up the connection. On success, you should get the message: “Welcome to GitLab, UserName!”

ssh -T -i ~/.ssh/GitLab_Robot_Deploy_Key git@gitlab.mycorp.com

Create the SSH Configuration File

Next, use an editor to create a ~/.ssh/config file. Add the following contents. The ‘Host’ value can be anything you want (just remember it, because you’ll be using it later). The HostName is the URL to your GitLab instance. The IdentifyFile is path to the SSH key file you created in the first step.

Host GitLab
  HostName gitlab.mycorp.com
  IdentityFile ~/.ssh/GitLab_Robot_Deploy_Key

Point SSH to the Configuration file

oxyum gave us the recipe for using pip with SSH:

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

We just need to modify it a bit to make SSH use our new Deploy Key. We do that by pointing SSH to the Host entry in the SSH configuration file. Just replace the ‘gitlab.mycorp.com’ in the command to the host name we used in the SSH configuration file:

pip install git+ssh://git@GitLab/my_name/my_repo.git

The package should now install without any password prompt.

Reference A
Reference B


回答 7

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

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

但是,由于我必须以pip as身份运行sudo,所以SSH密钥不再可与GitHub一起使用,并且“ git clone”在“权限被拒绝(公共密钥)”上失败。使用git+https允许我以sudo的身份运行命令,并让GitHub询问我的用户名/密码。

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

When I was installing from GitHub I was able to use:

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

But, since I had to run pip as sudo, the SSH keys were not working with GitHub any more, and “git clone” failed on “Permission denied (publickey)”. Using git+https allowed me to run the command as sudo, and have GitHub ask me for my user/password.

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

回答 8

您还可以通过提供登录凭据(登录名和密码,或部署令牌)通过git + https://github.com / … URL 安装私有存储库依赖关系,以使用该文件卷曲.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"

You can also install a private repository dependency via git+https://github.com/… URL by providing login credentials (login and password, or deploy token) for curl with the .netrc file:

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"

回答 9

如果要从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}

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

If you want to install dependencies from a requirements file within a CI server or alike, you can do this:

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

In my case, I used GIT_USER=gitlab-ci-token and GIT_PASS=${CI_JOB_TOKEN}.

This method has a clear advantage. You have a single requirements file which contains all of your dependencies.


回答 10

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

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

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

If you don’t want to use SSH, you could add the username and password in the HTTPS URL.

The code below assumes that you have a file called “pass” in the working directory that contains your password.

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

回答 11

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

然后你可以输入

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

oxyum’s solution is OK for this answer. I just want to point out that you need to be careful if you are installing using sudo as the keys must be stored for root too (for example, /root/.ssh).

Then you can type

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

回答 12

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

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

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

If you have your own library/package on GitHub, GitLab, etc., you have to add a tag to commit with a concrete version of the library, for example, v2.0, and then you can install your package:

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

This works for me. Other solutions haven’t worked for me.


回答 13

这是一种对我有用的快速方法。只需分叉存储库,并使用您自己的GitHub帐户进行安装即可

pip install git+https://github.com/yourName/repoName

Here’s a quick method that worked for me. Simply fork the repo and install it from your own GitHub account with

pip install git+https://github.com/yourName/repoName

回答 14

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

Just copy the remote from the original git clone command (or from git remote -v). You will get something like this:

  • Bitbucket: git+ssh://git@bitbucket.org:your_account/my_pro.git

  • GitHub: git+ssh://git@github.com:your_account/my_pro.git

Next, you need to replace : with / next to the domain name.

So install using:

pip install git+ssh://git@bitbucket.org/your_account/my_pro.git

回答 15

你可以试试

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

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

You may try

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

without ssh:.... That works for me.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。