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

[英]Is it possible to use pip to install a package from a private github repository?


As the title suggests I am trying to install a python package from a private github repo. For a public repository I can issue the following command which works fine:

正如标题所示,我正在尝试从一个私有的github repo上安装一个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+ssh hoping 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

Does anyone know if it what I am trying to achieve is even possible? If so can you please tell me how?

有人知道我想要达到的目标是否可能吗?如果是的话,你能告诉我怎么做吗?

12 个解决方案

#1


227  

You can use git+ssh URI scheme, but you MUST set username:

可以使用git+ssh URI模式,但必须设置用户名:

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

See git@ part into URI?

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

PS: Also read about deploy keys.

还可以阅读关于部署密钥的文章。

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 -v prints to a / character before using the remote's address in the pip command:

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

$ 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

#2


51  

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

作为一项附加技术,如果您在本地克隆了私有存储库,您可以这样做:

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

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

编辑:更及时地,你可以这样做(-e将意味着你不需要提交更改,在它们被反映出来之前):

pip install -e C:\repo\directory

#3


36  

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 django project, for instance.

这也适用于在需求中添加这一行。例如,django项目中的txt。

#4


20  

Also works with Bitbucket:

也适用于Bitbucket都:

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

Pip will use your SSH keys in this case.

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

#5


10  

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:

举个例子:

-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.

如果你只是想安装它。

#6


6  

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

当我在github上安装时,我可以使用:

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 anymore, "git clone" failed on "Permission denied (publickey)". Using git+https allowed me to run the command as sudo, and have github ask me my user/password.

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

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

#7


6  

I found it much easier to use tokens than SSH keys. I couldn't find much good documentation on this, so came across this solution mainly through trial & error. Further, installing from pip & 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 repos. So you need to point setuptools to tell setuptools that you're pointing to a git repo:

GitHub不(目前,截止到2016年8月)提供了一种简单的方法来获取私人repos的zip / tarball。因此,需要使用setuptools来告诉setuptools,您正在指向一个git repo:

from setuptools import setup
import os
# get 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 repos, 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
  • 对于私有repos,需要使用GitHub进行身份验证;我发现的最简单的方法是创建一个oauth标记,将其放入您的环境中,然后将其包含到URL中
  • You need to include some version number (here is 0) at the end of the link, even if there's no package on PyPI. This has to be a actual number, not a word.
  • 您需要在链接的末尾包含一些版本号(这里是0),即使PyPI上没有包。这必须是一个实际的数字,而不是一个词。
  • You need to preface with git+ to tell setuptools it's to clone the repo, rather than pointing at a zip / tarball
  • 您需要在git+的前言中告诉setuptools它是克隆repo,而不是指向zip / tarball
  • version can be a branch, a tag, or a commit hash
  • 版本可以是分支、标记或提交散列
  • You need to supply --process-dependency-links if installing from pip
  • 如果从pip安装,您需要提供——进程相关的链接

#8


5  

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

您还可以通过git+https://github.com/.安装一个私有的repo依赖项。通过使用.netrc文件为curl提供登录凭证(登录和密码或部署令牌)的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"

#9


3  

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 config 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密钥之外的密钥(在我的例子中,用于'bot ')进行部署。也许有人可以用GitHub来验证。

Create a New SSH key:

ssh-keygen -t rsa -C "GitLab_Robot_Deploy_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

文件应该显示为~/。ssh / GitLab_Robot_Deploy_Key和~ / . 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.

复制并粘贴~/.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 git@gitlab.mycorp.com

ssh - t -我~ /。ssh / GitLab_Robot_Deploy_Key git@gitlab.mycorp.com

Create the SSH Config 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.

接下来,使用编辑器创建~/。ssh / config文件。添加以下内容。“Host”值可以是您想要的任何值(只要记住它,因为稍后您将使用它)。主机名是到您的GitLab实例的URL。标识文件是第一步中创建的SSH密钥文件的路径。

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

Point SSH to the Config file

@oxyum gave us the recipe for using pip with SSH:

@oxyum给了我们使用pip与SSH的配方:

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

pip安装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 config file. Just replace the 'gitlab.mycorp.com' in the command to the Host name we used in the SSH config file:

我们只需要稍微修改一下它,让SSH使用我们的新部署键。我们通过将SSH指向SSH配置文件中的主机条目来实现这一点。只需将命令中的“gitlab.mycorp.com”替换为SSH配置文件中使用的主机名:

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

pip安装git + ssh:/ / git@GitLab / my_name / my_repo.git

The package should now install with no password prompt.

这个包现在应该没有密码提示。

Reference A
Reference B

参考引用B

#10


0  

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 (e.g. /root/.ssh).

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

Then you can type

然后你可以输入

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

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

#11


0  

you may try

你可以试一试

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

pip安装git + git@gitlab.mycorp.com/my_name/my_repo.git

without ssh:... That's work for me.

没有ssh:…这对我的工作。

#12


0  

If you have your own library/packet on github/gitlab etc, you have to add tag to commit with concrete version of library e.g. v2.0 then you can install your packet

如果您在github/gitlab等上有自己的库/包,您必须添加标记以提交具体版本的库,例如v2.0,那么您可以安装您的包

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

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

这适合我。其他的解决方法对我不起作用。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/01/28/555af71167c20d1f10356671a2d32099.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告