首页 文章

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

提问于
浏览
252

正如 Headers 所示,我试图从私有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

有谁知道我想要达到的目标是否可能?如果是这样,请告诉我怎么样?

13 回答

  • 7

    你可以使用 git+ssh URI方案,但你必须设置用户名:

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

    请参阅 git@ 部分到URI?

    PS:另请阅读deploy keys .

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

    pip install -e URI#egg=EggName
    

    Remember :在 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
    
  • 0

    作为一种额外的技术,如果您在本地克隆了私有存储库,则可以执行以下操作:

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

    编辑:更现代,你可以这样做( -e 将意味着你不反映):

    pip install -e C:\repo\directory
    
  • 40

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

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

    例如,这也可以在django项目中的requirements.txt中附加该行 .

  • 7

    也适用于Bitbucket:

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

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

  • 258

    需求文件的语法如下:

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

    如果你只是想安装它 .

  • 12

    我发现使用令牌比使用SSH密钥要容易得多 . 我找不到很多关于此的好文档,所以主要通过反复试验来解决这个问题 . 此外,从pip和setuptools安装有一些细微的差别;但这种方式应该适用于两者 .

    GitHub没有(目前,截至2016年8月)提供了一个简单的方法来获取私人回购的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)
            ]
    

    这里有几点说明:

    • 对于私人回购,您需要使用GitHub进行身份验证;我找到的最简单的方法是创建一个oauth令牌,将其放入您的环境中,然后将其包含在URL中

    • 您需要在链接末尾包含一些版本号(这里是 0 ),即使PyPI上没有包 . 这必须是实际数字,而不是单词 .

    • 你需要在 git+ 前面告诉setuptools克隆repo,而不是指向zip / tarball

    • version 可以是分支,标记或提交哈希

    • 如果从pip安装,则需要提供 --process-dependency-links

  • 56

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

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

    但是,因为我必须运行pip为 sudo ,所以SSH密钥不再使用github,"git clone"在"Permission denied (publickey)"上失败了 . 使用 git+https 允许我以sudo的身份运行命令,并让github问我的用户名/密码 .

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

    我想出了一种自动“pip安装”GitLab私有存储库的方法,该存储库不需要密码提示 . 此方法使用GitLab“Deploy Keys”和SSH配置文件,因此您可以使用除个人SSH密钥之外的密钥进行部署(在我的情况下,供'bot使用) . 也许某个善良的灵魂可以使用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 "Deploy Keys"对话框中 .

    测试新部署密钥

    以下命令告诉SSH使用新的部署密钥来设置连接 . 成功之后,您应该收到消息:“欢迎使用GitLab,UserName!”

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

    创建SSH配置文件

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

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

    将SSH指向Config文件

    @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

    该软件包现在应该安装时没有密码提示 .

    Reference A
    Reference B

  • 5

    您还可以通过git+https://github.com/... URL为curl提供登录凭据(登录名和密码或部署令牌)来安装私有仓库依赖关系:

    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"
    
  • 8

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

    然后你可以输入

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

  • 0

    你可以试试

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

    没有ssh:......这对我有用 .

  • 23

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

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

    这适合我 . 其他解决方案对我没用 .

  • 0

    如果您不想使用ssh,可以在https网址中添加用户名和密码 .
    下面的代码假定您在工作目录中有一个名为"pass"的文件,其中包含您的密码 .

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

相关问题