Python 是否可以使用 pip 从私有 GitHub 存储库安装包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4830856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:43:25  来源:igfitidea点击:

Is it possible to use pip to install a package from a private GitHub repository?

pythongitgithubpip

提问by Adam J. Forster

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:

我正在尝试从私有 GitHub 存储库安装 Python 包。对于公共存储库,我可以发出以下可以正常工作的命令:

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 + sshhoping that pip would use my SSH public key to authenticate:

我想这是因为我试图在不提供任何身份验证的情况下访问私有存储库。因此,我尝试使用 Git +ssh希望 pip 使用我的 SSH 公钥进行身份验证:

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?

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

采纳答案by oxyum

You can use the git+sshURI scheme, but you mustset a username:

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

pip install git+ssh://[email protected]/echweb/echweb-utils.git

Do you see the git@part into the URI?

您看到git@URI 中的部分了吗?

PS: Also read about deploy keys.

PS:另请阅读部署密钥

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

PPS:在我的安装中,“git+ssh”URI 方案仅适用于“可编辑”要求:

pip install -e URI#egg=EggName

Remember: Change the :character that git remote -vprints to a /character before using the remote's address in the pipcommand:

请记住:在命令中使用远程地址之前:,将git remote -v打印的/字符更改为字符pip

$ git remote -v
origin  [email protected]: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

回答by gnrfan

It also works with Bitbucket:

它也适用于Bitbucket

pip install git+ssh://[email protected]/username/projectname.git

Pip will use your SSH keys in this case.

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

回答by Rachel

The syntax for the requirements file is given here:

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

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

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.

如果您只想安装它。

回答by Scott Stafford

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 -ewill mean you don't have to commit changes before they're reflected):

更现代地,您可以这样做(这-e意味着您不必在更改反映之前提交更改):

pip install -e C:\repo\directory

回答by danius

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

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

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

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

例如,这也适用于在Django项目的 requirements.txt 中附加该行。

回答by jcarballo

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

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

pip install git+ssh://[email protected]/<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+httpsallowed me to run the command as sudo, and have GitHub ask me for my user/password.

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

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

回答by Andres Romero

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

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

Then you can type

然后你可以输入

sudo pip install git+ssh://[email protected]/echweb/echweb-utils.git

回答by Maximilian

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.

我发现使用令牌比使用 SSH 密钥容易得多。我在这方面找不到太多好的文档,所以我主要通过反复试验找到了这个解决方案。此外,从 pip 和 setuptools 安装有一些细微的区别;但这种方式应该对两者都有效。

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:

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

A couple of notes here:

这里有几个注意事项:

  • For private repositories, you need to authenticate with GitHub; the simplest way I found is to create an OAuthtoken, drop that into your environment, and then include it with the URL
  • You need to include someversion 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
  • versioncan be a branch, a tag, or a commit hash
  • You need to supply --process-dependency-linksif installing from pip
  • 对于私有仓库,您需要通过 GitHub 进行身份验证;我发现的最简单的方法是创建一个OAuth令牌,将其放入您的环境中,然后将其包含在 URL 中
  • 您需要在链接末尾包含一些版本号(这里是0),即使PyPI上没有任何包。这必须是一个实际数字,而不是一个词。
  • 您需要先git+告诉 setuptools 它是克隆存储库,而不是指向 zip / tarball
  • version可以是分支、标签或提交哈希
  • --process-dependency-links如果从 pip 安装,则需要提供

回答by ei-grad

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 curlwith the .netrcfile:

您还可以通过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"

回答by JS.

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.

我想出了一种无需密码提示的自动“pip 安装”GitLab 私有存储库的方法。这种方法使用 GitLab“部署密钥”和 SSH 配置文件,因此您可以使用除个人 SSH 密钥以外的密钥进行部署(在我的情况下,供“机器人”使用)。也许有人可以使用GitHub进行验证。

Create a New SSH key:

创建一个新的 SSH 密钥:

ssh-keygen -t rsa -C "GitLab_Robot_Deploy_Key"

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

该文件应显示为~/.ssh/GitLab_Robot_Deploy_Key~/.ssh/GitLab_Robot_Deploy_Key.pub

Copy and paste the contents of the ~/.ssh/GitLab_Robot_Deploy_Key.pubfile into the GitLab "Deploy Keys" dialog.

~/.ssh/GitLab_Robot_Deploy_Key.pub文件内容复制并粘贴到 GitLab“部署密钥”对话框中。

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 使用您的新部署密钥来设置连接。成功后,您应该收到消息:“欢迎使用 GitLab,用户名!”

ssh -T -i ~/.ssh/GitLab_Robot_Deploy_Key [email protected]

Create the SSH Configuration File

创建 SSH 配置文件

Next, use an editor to create a ~/.ssh/configfile. 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.

接下来,使用编辑器创建~/.ssh/config文件。添加以下内容。'Host' 值可以是您想要的任何值(请记住它,因为您稍后会使用它)。HostName 是 GitLab 实例的 URL。IdentificationFile 是您在第一步中创建的 SSH 密钥文件的路径。

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

Point SSH to the Configuration file

将 SSH 指向配置文件

oxyum gave usthe recipe for using pip with SSH:

oxyum 给了我们在 SSH 中使用 pip 的秘诀:

pip install git+ssh://[email protected]/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:

我们只需要稍微修改一下就可以让 SSH 使用我们新的 Deploy Key。我们通过将 SSH 指向 SSH 配置文件中的 Host 条目来实现。只需将命令中的 'gitlab.mycorp.com' 替换为我们在 SSH 配置文件中使用的主机名:

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

The package should now install without any password prompt.

该软件包现在应该在没有任何密码提示的情况下安装。

Reference A
Reference B

参考 A
参考 B