是否可以使用 pip 从私有 GitHub 存储库安装包?
我正在尝试从私有 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
我想要实现的目标是否可能?如果是这样,我该怎么办?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
如果您不想使用 SSH,可以在 HTTPS URL 中添加用户名和密码。
下面的代码假设您的工作目录中有一个名为“pass”的文件,其中包含您的密码。
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.
如果您在 GitHub、GitLab 等上有自己的库/包,则必须添加一个标签来提交库的具体版本,例如 v2.0,然后您可以安装您的包:
这适用于我。其他解决方案对我不起作用。
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:
This works for me. Other solutions haven't worked for me.
只需从原始 git clone 命令(或从 git remote -v )复制远程即可。您将得到如下内容:
Bitbucket:
git+ssh://[电子邮件受保护]:your_account/my_pro.git
< strong>GitHub:
git+ssh://[电子邮件受保护]:your_account/my_pro.git
接下来,您需要替换
:
并在域名旁边添加
/
。因此安装使用:
Just copy the remote from the original
git clone
command (or fromgit remote -v
). You will get something like this:Bitbucket:
git+ssh://[email protected]:your_account/my_pro.git
GitHub:
git+ssh://[email protected]:your_account/my_pro.git
Next, you need to replace
:
with/
next to the domain name.So install using:
oxyum 的解决方案适合这个答案。我只是想指出,如果使用
sudo
安装,则需要小心,因为密钥也必须为 root 存储(例如,/root/.ssh
)。然后你可以输入
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
您可以尝试
不使用
ssh:...
。这对我有用。You may try
without
ssh:...
. That works for me.您可以使用
git+ssh
URI 方案,但您必须设置用户名。请注意 URI 中的git@
部分:另请参阅 部署密钥。
PS:在我的安装中,“git+ssh”URI 方案仅适用于“可编辑”要求:
记住:更改
:
字符git remote -v
在pip
命令中使用远程地址之前打印到/
字符:如果您忘记了,您将收到此错误:
You can use the
git+ssh
URI scheme, but you must set a username. Notice thegit@
part in the URI:Also read about deploy keys.
PS: In my installation, the "git+ssh" URI scheme works only with "editable" requirements:
Remember: Change the
:
character thatgit remote -v
prints to a/
character before using the remote's address in thepip
command:If you forget, you will get this error:
作为一种附加技术,如果您在本地克隆了私有存储库,您可以执行以下操作:
更现代的是,您可以执行此操作(并且
-e
意味着您不必在更改之前提交更改)反映):As an additional technique, if you have the private repository cloned locally, you can do:
More modernly, you can just do this (and the
-e
will mean you don't have to commit changes before they're reflected):您可以直接使用 HTTPS URL 来完成此操作,如下所示:
这也只需在 Django 项目。
You can do it directly with the HTTPS URL like this:
This also works just appending that line in the requirements.txt in a Django project, for instance.
它也适用于 Bitbucket:
在这种情况下,Pip 将使用您的 SSH 密钥。
It also works with Bitbucket:
Pip will use your SSH keys in this case.
我发现使用令牌比 SSH 密钥容易得多。我找不到关于此的太多好的文档,因此我主要通过反复试验找到了这个解决方案。此外,从 pip 和 setuptools 安装有一些细微的差别;但这种方式应该对双方都有效。
GitHub(目前,截至 2016 年 8 月)不提供获取私有存储库的 zip/tarball 的简单方法。因此,您需要告诉 setuptools 您正在指向 Git 存储库:
这里有一些注意事项:
0
),即使PyPI。这必须是一个实际的数字,而不是一个单词。git+
开头,告诉 setuptools 它要克隆存储库,而不是指向 zip / tarballversion
可以是分支、标签或提交哈希--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:
A couple of notes here:
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.git+
to tell setuptools it's to clone the repository, rather than pointing at a zip / tarballversion
can be a branch, a tag, or a commit hash--process-dependency-links
if installing from pip我找到了一种自动“pip 安装”GitLab 私有存储库的方法,无需密码提示。此方法使用 GitLab“部署密钥”和 SSH 配置文件,因此您可以使用个人 SSH 密钥以外的密钥进行部署(在我的例子中,供“机器人”使用)。也许有好心人可以使用 GitHub 进行验证。
创建新的 SSH 密钥:
该文件应显示为
~/.ssh/GitLab_Robot_Deploy_Key
和~/.ssh/GitLab_Robot_Deploy_Key.pub
。将
~/.ssh/GitLab_Robot_Deploy_Key.pub
文件的内容复制并粘贴到 GitLab“部署密钥”对话框中。测试新的部署密钥
以下命令告诉 SSH 使用新的部署密钥来设置连接。成功后,您应该收到消息:“欢迎来到 GitLab,用户名!”
创建 SSH 配置文件
接下来,使用编辑器创建
~/.ssh/config
文件。添加以下内容。 “Host”值可以是您想要的任何值(只需记住它,因为稍后您将使用它)。 HostName 是您的 GitLab 实例的 URL。 IdentityFile 是您在第一步中创建的 SSH 密钥文件的路径。将 SSH 指向配置文件
oxyum 为我们提供了在 SSH 中使用 pip 的秘诀:
我们只需要对其进行一些修改即可使 SSH 使用我们新的部署密钥。我们通过将 SSH 指向 SSH 配置文件中的主机条目来做到这一点。只需将命令中的“gitlab.mycorp.com”替换为我们在 SSH 配置文件中使用的主机名即可:
现在应该安装软件包,而无需任何密码提示。
参考 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:
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!"
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.Point SSH to the Configuration file
oxyum gave us the recipe for using pip with SSH:
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:
The package should now install without any password prompt.
Reference A
Reference B
如果您想从 CI 服务器中的需求文件安装依赖项,可以执行以下操作:
在我的例子中,我使用了 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:
In my case, I used
GIT_USER=gitlab-ci-token
andGIT_PASS=${CI_JOB_TOKEN}
.This method has a clear advantage. You have a single requirements file which contains all of your dependencies.
如果您需要在命令行中执行此操作,那么也是可以的。我能够在 Google Colab 上进行部署:
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:
pip install git+https://<USERNAME>:<PERSONAL ACCESS TOKEN>@github.com/<ACCOUNT>/<REPOSITORY>.git
我的情况比答案中描述的大多数情况都要复杂一些。我是 Github 组织中两个私有存储库
repo_A
和repo_B
的所有者,需要在python
期间pip install repo_A
repo_B
的代码> 单元测试,作为 Github 操作。我为解决此任务而遵循的步骤:
存储库密钥 >repo_B
,将我的个人访问令牌粘贴到其中并将其命名为PERSONAL_ACCESS_TOKEN
。 这很重要,因为与 Jamie 提出的解决方案不同,我不需要明确在 github 操作.yml
文件中公开我珍贵的原始个人访问令牌。pip install
软件包,如下所示: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
andrepo_B
in a Github organization and needed topip install repo_A
during thepython
unittests ofrepo_B
, as a Github action.Steps I followed to solve this task:
repo_B
, pasted my Personal Access Token in there and named itPERSONAL_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.pip install
the package from source via HTTPS (not SSH) as follows:需求文件的语法如下:
https: //pip.pypa.io/en/latest/reference/pip_install.html#requirements-file-format
例如,
如果您希望源在安装后保留下来,
请使用:或者只是
如果您只想安装它。
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:
if you want the source to stick around after installation.
Or just
if you just want it to be installed.
您还可以通过 git+https://github.com/ 安装私有存储库依赖项。 .. URL,通过提供 curl 与
.netrc
文件: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:当我从 GitHub 安装时,我能够使用:
但是,由于我必须以
sudo
身份运行 pip,SSH 密钥不再适用于 GitHub,并且“git clone”在“权限”上失败被拒绝(公钥)”。使用 git+https 允许我以 sudo 身份运行命令,并让 GitHub 询问我的用户名/密码。When I was installing from GitHub I was able to use:
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)". Usinggit+https
allowed me to run the command as sudo, and have GitHub ask me for my user/password.