Python setuptools:如何在 install_requires 下列出私有存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18026980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python setuptools: How can I list a private repository under install_requires?
提问by Eric P
I am creating a setup.py
file for a project which depends on private GitHub repositories. The relevant parts of the file look like this:
我正在setup.py
为依赖于私有 GitHub 存储库的项目创建一个文件。文件的相关部分如下所示:
from setuptools import setup
setup(name='my_project',
...,
install_requires=[
'public_package',
'other_public_package',
'private_repo_1',
'private_repo_2',
],
dependency_links=[
'https://github.com/my_account/private_repo_1/master/tarball/',
'https://github.com/my_account/private_repo_2/master/tarball/',
],
...,
)
I am using setuptools
instead of distutils
because the latter does not support the install_requires
and dependency_links
arguments per thisanswer.
我使用setuptools
而不是distutils
因为后者不支持这个答案的install_requires
和dependency_links
参数。
The above setup file fails to access the private repos with a 404 error - which is to be expected since GitHub returns a 404 to unauthorized requests for a private repository. However, I can't figure out how to make setuptools
authenticate.
上面的设置文件无法访问私有存储库并出现 404 错误 - 这是意料之中的,因为 GitHub 向未授权的私有存储库请求返回 404。但是,我无法弄清楚如何进行setuptools
身份验证。
Here are some things I've tried:
以下是我尝试过的一些事情:
Use
git+ssh://
instead ofhttps://
independency_links
as I would if installing the repo withpip
. This fails because setuptools doesn't recognize this protocol ("unknown url type: git+ssh"), though the distribute documentationsays it should. Dittogit+https
andgit+http
.https://<username>:<password>@github.com/...
- still get a 404. (This method doesn't work withcurl
orwget
from the command line either - thoughcurl -u <username> <repo_url> -O <output_file_name>
does work.)Upgrading setuptools (0.9.7) and virtualenv (1.10) to the latest versions. Also tried installing distribute though this overviewsays it was merged back into setuptools. Either way, no dice.
使用
git+ssh://
的,而不是https://
在dependency_links
为我想如果安装与回购pip
。这失败是因为 setuptools 无法识别此协议(“未知 url 类型:git+ssh”),尽管分发文档说它应该。同上git+https
和git+http
。https://<username>:<password>@github.com/...
- 仍然得到 404。(此方法也不适用于命令行curl
或wget
从命令行运行 - 尽管curl -u <username> <repo_url> -O <output_file_name>
确实有效。)将 setuptools (0.9.7) 和 virtualenv (1.10) 升级到最新版本。还尝试安装分发,尽管此概述说它已合并回 setuptools。无论哪种方式,都没有骰子。
Currently I just have setup.py
print out a warning that the private repos must be downloaded separately. This is obviously less than ideal. I feel like there's something obvious that I'm missing, but can't think what it might be. :)
目前我只是setup.py
打印出一个警告,必须单独下载私有存储库。这显然不太理想。我觉得我缺少一些明显的东西,但无法想象它可能是什么。:)
Duplicate-ish question with no answers here.
这里没有答案的重复问题。
采纳答案by Tom Hemmes
I was trying to get this to work for installing with pip, but the above was not working for me. From [1] I understood the PEP508
standard should be used, from [2] I retrieved an example which actually does work (at least for my case).
我试图让它使用 pip 进行安装,但上述内容对我不起作用。从 [1] 我理解PEP508
应该使用标准,从 [2] 我检索了一个实际工作的示例(至少对于我的情况)。
Please note; this is with pip 20.0.2
on Python 3.7.4
请注意; 这与pip 20.0.2
上Python 3.7.4
setup(
name='<package>',
...
install_requires=[
'<normal_dependency>',
# Private repository
'<dependency_name> @ git+ssh://[email protected]/<user>/<repo_name>@<branch>',
# Public repository
'<dependency_name> @ git+https://github.com/<user>/<repo_name>@<branch>',
],
)
After specifying my package this way installation works fine (also with -e
settings and without the need to specify --process-dependency-links
).
以这种方式指定我的包后,安装工作正常(也有-e
设置,无需指定--process-dependency-links
)。
References[1] https://github.com/pypa/pip/issues/4187[2] https://github.com/pypa/pip/issues/5566
参考文献[1] https://github.com/pypa/pip/issues/4187[2] https://github.com/pypa/pip/issues/5566
回答by wor
Edit: This appears to only work with public github repositories, see comments.
编辑:这似乎只适用于公共 github 存储库,请参阅评论。
dependency_links=[
'https://github.com/my_account/private_repo_1/tarball/master#egg=private_repo_1',
'https://github.com/my_account/private_repo_2/tarball/master#egg=private_repo_2',
],
Above syntax seems to work for me with setuptools 1.0. At the moment at least the syntax of adding "#egg=project_name-version" to VCS dependencies is documented in the link you gave to distribute documentation.
使用 setuptools 1.0,以上语法似乎对我有用。目前,至少将“#egg=project_name-version”添加到 VCS 依赖项的语法记录在您提供的分发文档链接中。
回答by vadimg
Here's what worked for me:
以下是对我有用的内容:
install_requires=[
'private_package_name==1.1',
],
dependency_links=[
'git+ssh://[email protected]/username/private_repo.git#egg=private_package_name-1.1',
]
Note that you have to have the version number in the egg name, otherwise it will say it can't find the package.
注意egg名称中必须有版本号,否则会说找不到包。
回答by Overclocked
Using archive URL from github works for me, for public repositories. E.g.
对于公共存储库,使用来自 github 的存档 URL 对我有用。例如
dependency_links = [
'https://github.com/username/reponame/archive/master.zip#egg=eggname-version',
]
回答by cjohnson318
I found a (hacky) workaround:
我找到了一个(hacky)解决方法:
#!/usr/bin/env python
from setuptools import setup
import os
os.system('pip install git+https://github-private.corp.com/user/repo.git@master')
setup( name='original-name'
, ...
, install_requires=['repo'] )
I understand that there are ethical issues with having a system call in a setup script, but I can't think of another way to do this.
我知道在设置脚本中进行系统调用存在道德问题,但我想不出另一种方法来做到这一点。
回答by Maximilian
I couldn't find any good documentation on this, but came across the solution mainly through trial & error. Further, installing from pip & setuptools have some subtle differences; but this way should work for both.
我找不到任何关于此的好的文档,但主要通过反复试验找到了解决方案。此外,从 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 月)提供一种简单的方法来获取私有存储库的 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
- You need to include someversion 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. - You need to preface with
git+
to tell setuptools it's to clone the repo, 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
- 对于私有仓库,需要通过 GitHub 进行身份验证;我发现的最简单的方法是创建一个 oauth 令牌,将其放入您的环境中,然后将其包含在 URL 中
- 您需要在链接末尾包含一些版本号(这里是
0
),即使 PyPI 上没有包。这必须是一个实际数字,而不是一个词。 - 您需要先
git+
告诉 setuptools 它是克隆 repo,而不是指向 zip/tarball version
可以是分支、标签或提交哈希--process-dependency-links
如果从 pip 安装,则需要提供
回答by kotrfa
This work for our scenario:
这适用于我们的场景:
- package is on github in a private repo
- we want to install it into site-packages (not into ./src with -e)
- being able to use pip install -r requirements.txt
- being able to use pip install -e reposdir (or from github), where the dependencies are only specified in requirements.txt
- 软件包位于 github 上的私有仓库中
- 我们想将它安装到站点包中(而不是使用 -e 安装到 ./src 中)
- 能够使用 pip install -r requirements.txt
- 能够使用 pip install -e reposdir(或来自 github),其中依赖项仅在 requirements.txt 中指定
https://github.com/pypa/pip/issues/3610#issuecomment-356687173
https://github.com/pypa/pip/issues/3610#issuecomment-356687173
回答by PidgeyBE
Via Tom Hemmes' answer I found this is the only thing that worked for me:
通过 Tom Hemmes 的回答,我发现这是唯一对我有用的方法:
install_requires=[
'<package> @ https://github.com/<username>/<package>/archive/<branch_name>.zip']