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

发布于 2024-10-14 12:27:12 字数 1975 浏览 5 评论 0 原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

指尖微凉心微凉 2024-10-21 12:27:13

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

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

export PASS=$(cat pass)
pip install git+https://<username>:[email protected]/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>:[email protected]/echweb/echweb-utils.git
窝囊感情。 2024-10-21 12:27:13

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

pip install git+ssh://link/name/[email protected]

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

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/[email protected]

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

笑红尘 2024-10-21 12:27:13

只需从原始 git clone 命令(或从 git remote -v )复制远程即可。您将得到如下内容:

接下来,您需要替换 : 并在域名旁边添加 /

因此安装使用:

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

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

So install using:

pip install git+ssh://[email protected]/your_account/my_pro.git
寄风 2024-10-21 12:27:13

oxyum 的解决方案适合这个答案。我只是想指出,如果使用 sudo 安装,则需要小心,因为密钥也必须为 root 存储(例如,/root/.ssh )。

然后你可以输入

sudo pip install git+ssh://[email protected]/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://[email protected]/echweb/echweb-utils.git
情绪 2024-10-21 12:27:13

您可以尝试

pip install [email protected]/my_name/my_repo.git

不使用 ssh:...。这对我有用。

You may try

pip install [email protected]/my_name/my_repo.git

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

巴黎夜雨 2024-10-21 12:27:12

您可以使用 git+ssh URI 方案,但您必须设置用户名。请注意 URI 中的 git@ 部分:

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

另请参阅 部署密钥

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

pip install -e URI#egg=EggName

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

$ git remote -v
origin  [email protected]: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. Notice the git@ part in the URI:

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

Also read about deploy keys.

PS: 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  [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
梦幻的心爱 2024-10-21 12:27:12

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

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
回梦 2024-10-21 12:27:12

您可以直接使用 HTTPS URL 来完成此操作,如下所示:

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

这也只需在 Django 项目。

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.

悍妇囚夫 2024-10-21 12:27:12

它也适用于 Bitbucket

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

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

It also works with Bitbucket:

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

Pip will use your SSH keys in this case.

月朦胧 2024-10-21 12:27:12

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

GitHub(目前,截至 2016 年 8 月)不提供获取私有存储库的 zip/tarball 的简单方法。因此,您需要告诉 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
  • 您需要在链接末尾包含一些版本号(这里是0),即使PyPI。这必须是一个实际的数字,而不是一个单词。
  • 您需要以 git+ 开头,告诉 setuptools 它要克隆存储库,而不是指向 zip / tarball
  • version 可以是分支、标签或提交哈希
  • 如果从 pip 安装,您需要提供 --process-dependency-links

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 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
醉态萌生 2024-10-21 12:27:12

我找到了一种自动“pip 安装”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 [email protected]

创建 SSH 配置文件

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

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

将 SSH 指向配置文件

oxyum 为我们提供了在 SSH 中使用 pip 的秘诀:

pip install git+ssh://[email protected]/my_name/my_repo.git

我们只需要对其进行一些修改即可使 SSH 使用我们新的部署密钥。我们通过将 SSH 指向 SSH 配置文件中的主机条目来做到这一点。只需将命令中的“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 [email protected]

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://[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:

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

The package should now install without any password prompt.

Reference A
Reference B

燕归巢 2024-10-21 12:27:12

如果您想从 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-token 和 GIT_PASS=${CI_JOB_TOKEN} 。

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

If you want to install dependencies from a requirements file within a CI server, 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.

非要怀念 2024-10-21 12:27:12

如果您需要在命令行中执行此操作,那么也是可以的。我能够在 Google Colab 上进行部署:

  1. 创建个人访问令牌: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
  2. 运行:pip install git+https://<用户名>:<个人访问令牌>@github.com/<帐户>/<存储库>.git

If you need to do this in, say, a command line one-liner, it's also possible. I was able to do this for deployment on Google Colab:

  1. Create a Personal Access Token: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
  2. Run: pip install git+https://<USERNAME>:<PERSONAL ACCESS TOKEN>@github.com/<ACCOUNT>/<REPOSITORY>.git
作死小能手 2024-10-21 12:27:12

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

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

  • 创建了 我的帐户的个人访问令牌。至于它的权限,我只需要保留默认的,即repo - 完全控制私有存储库
  • 存储库密钥 >repo_B,将我的个人访问令牌粘贴到其中并将其命名为 PERSONAL_ACCESS_TOKEN这很重要,因为与 Jamie 提出的解决方案不同,我不需要明确在 github 操作 .yml 文件中公开我珍贵的原始个人访问令牌。
  • 最后,通过 HTTPS(不是 SSH)从源pip install 软件包,如下所示:
export PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }}

pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git

My case was kind of more complicated than most of the ones described in the answers. I was the owner of two private repositories repo_A and repo_B in a Github organization and needed to pip install repo_A during the python unittests of repo_B, as a Github action.

Steps I followed to solve this task:

  • Created a Personal Access Token for my account. As for its permissions, I only needed to keep the default ones, .i.e. repo - Full control of private repositories.
  • Created a repository secret under repo_B, pasted my Personal Access Token in there and named it PERSONAL_ACCESS_TOKEN. This was important because, unlike the solution proposed by Jamie, I didn't need to explicitly expose my precious raw Personal Access Token inside the github action .yml file.
  • Finally, pip install the package from source via HTTPS (not SSH) as follows:
export PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }}

pip install git+https://${PERSONAL_ACCESS_TOKEN}@github.com/MY_ORG_NAME/repo_A.git
人间不值得 2024-10-21 12:27:12

需求文件的语法如下:

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

如果您只想安装它。

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.

不即不离 2024-10-21 12:27:12

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

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"
痕至 2024-10-21 12:27:12

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

pip install git+ssh://[email protected]/<username>/<projectname>.git#egg=<eggname>

但是,由于我必须以 sudo 身份运行 pip,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://[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+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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文