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

Ada*_*ter 321 python git pip github

正如标题所示,我试图从私有github仓库安装一个python包.对于公共存储库,我可以发出以下命令,该命令工作正常:

pip install git+git://github.com/django/django.git
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用私有存储库:

pip install git+git://github.com/echweb/echweb-utils.git
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

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
Run Code Online (Sandbox Code Playgroud)

我想这是因为我试图访问私有存储库而不提供任何身份验证.因此我尝试使用git + ssh希望pip使用我的ssh公钥进行身份验证:

pip install git+ssh://github.com/echweb/echweb-utils.git
Run Code Online (Sandbox Code Playgroud)

这给出了以下输出:

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
Run Code Online (Sandbox Code Playgroud)

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

oxy*_*yum 329

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

pip install git+ssh://git@github.com/echweb/echweb-utils.git
Run Code Online (Sandbox Code Playgroud)

请参阅git@URI部分?

PS:还阅读有关部署密钥的信息.

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

pip install -e URI#egg=EggName
Run Code Online (Sandbox Code Playgroud)

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

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

如果您忘记了,您将收到此错误:

ssh: Could not resolve hostname github.com:echweb:
         nodename nor servname provided, or not known
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,我只是错过了git @ part.顺便说一下命令'pip install git + ssh://git@github.com/echweb/echweb-utils.git'工作,我不需要-e开关. (3认同)
  • 如果您需要从脚本运行它(例如:用于部署...),您可以指定使用哪个 SSH 密钥与环境变量 `GIT_SSH_COMMAND="ssh -i ~/.ssh/my-deploy-key"` (3认同)
  • 您还可以使用.ssh / config文件设置正确的用户名 (2认同)
  • 这曾经对我有用,但现在不是,我甚至在我的需求文件中使用正确的`git + ssh:// git @ github.com/echweb/echweb-utils.git`方案.我在这里打开了一个新问题:http://stackoverflow.com/questions/18883430/trouble-installing-private-github-repository-using-pip.任何帮助都会很棒. (2认同)
  • 完善.把`git + ssh:// git @ github.com/echweb/echweb-utils.git`放到`requirements.txt`中也很有用,这太棒了. (2认同)
  • 如果要从特定分支安装:`pip install git + ssh:// git @ github.com/echweb/echweb-utils.git @ branch-name` (2认同)

Sco*_*ord 69

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

pip install git+file://c:/repo/directory
Run Code Online (Sandbox Code Playgroud)

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

pip install -e C:\repo\directory
Run Code Online (Sandbox Code Playgroud)

  • 这非常有帮助.显然,本地更改必须在通过pip安装之前进行git-commit. (9认同)
  • 这是真的 - 它将它从git存储库(在.git中)中拉出来,而不是文件的工作副本. (5认同)

dan*_*ius 46

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

pip install git+https://github.com/username/repo.git
Run Code Online (Sandbox Code Playgroud)

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

  • 但是,对于私有存储库,这将在控制台上触发用户名/密码提示,这可能不是您想要的. (13认同)

小智 26

也适用于Bitbucket:

pip install git+ssh://git@bitbucket.org/username/projectname.git
Run Code Online (Sandbox Code Playgroud)

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


pck*_*ko1 19

我的情况比答案中描述的大多数情况都要复杂一些。repo_A我是两个私有存储库和一个 Github 组织的所有者repo_B,并且需要pip install repo_Apython单元测试repo_B期间作为 Github 操作

我为解决此任务而遵循的步骤:

  • 为我的帐户创建了个人访问令牌。至于它的权限,我只需要保留默认的, .ie repo - Full control of private repositories
  • 在下面创建了一个存储库机密repo_B,将我的个人访问令牌粘贴到其中并命名PERSONAL_ACCESS_TOKEN这很重要,因为与Jamie提出的解决方案不同,我不需要在 github 操作.yml文件中显式公开我宝贵的原始个人访问令牌。
  • 最后,通过 HTTPS(不是pip installSSH)从源进行打包,如下所示:
export PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }}

pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下 SSH 密钥也能起作用吗? (3认同)

Ami*_*ein 16

如果要从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
Run Code Online (Sandbox Code Playgroud)

就我而言,我使用了GIT_USER=gitlab-ci-tokenGIT_PASS=${CI_JOB_TOKEN}

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


Rac*_*hel 14

需求文件的语法如下:

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

例如:

-e git+http://github.com/rwillmer/django-behave#egg=django-behave
Run Code Online (Sandbox Code Playgroud)

如果您希望源安装后留下来

要不就

git+http://github.com/rwillmer/django-behave#egg=django-behave
Run Code Online (Sandbox Code Playgroud)

如果你只是想安装它.


JS.*_*JS. 14

我想出了一种自动"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文件.添加以下内容."主机"值可以是您想要的任何值(只需记住它,因为您稍后将使用它).HostName是GitLab实例的URL.IdentifyFile是您在第一步中创建的SSH密钥文件的路径.

Host GitLab
  HostName gitlab.mycorp.com
  IdentityFile ~/.ssh/GitLab_Robot_Deploy_Key
Run Code Online (Sandbox Code Playgroud)

将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

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

参考A
参考B.


Max*_*ian 13

我发现使用令牌比使用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)
        ]
Run Code Online (Sandbox Code Playgroud)

这里有几点说明:

  • 对于私人回购,您需要使用GitHub进行身份验证; 我找到的最简单的方法是创建一个oauth令牌,将其放入您的环境中,然后将其包含在URL中
  • 您需要在链接末尾包含一些版本号(此处为0),即使PyPI上没有包.这必须是实际数字,而不是单词.
  • 你需要在序言git+中告诉setuptools克隆repo,而不是指向zip/tarball
  • version 可以是分支,标记或提交哈希
  • --process-dependency-links如果从pip安装,你需要提供

  • 随着 pip 版本 19.0 的发布,--process-dependency-links 选项已被弃用。 (2认同)

Jam*_*mie 9

如果您需要在命令行单行程序中执行此操作,也可以这样做。我能够在 Google Colab 上进行部署:

  1. 创建个人访问令牌:https : //docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
  2. 跑: pip install git+https://<PERSONAL ACCESS TOKEN>@github.com/<USERNAME>/<REPOSITORY>.git


小智 8

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

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

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

  • 这是否会将您的密码以明文形式标记到某个配置文件中? (3认同)

jca*_*llo 7

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

pip install git+ssh://git@github.com/<username>/<projectname>.git#egg=<eggname>
Run Code Online (Sandbox Code Playgroud)

但是,因为我还得跑PIP作为sudo,SSH密钥没有用github上工作了,"混帐克隆"的"权限被拒绝(公钥)"失败.使用git+https允许我以sudo运行命令,并让github问我我的用户/密码.

sudo pip install git+https://github.com/<username>/<projectname>.git#egg=<eggname>
Run Code Online (Sandbox Code Playgroud)


ei-*_*rad 5

您还可以通过git + https://github.com/... URL 安装私有repo依赖项,方法是为文件提供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"
Run Code Online (Sandbox Code Playgroud)