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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:45:49  来源:igfitidea点击:

Python setuptools: How can I list a private repository under install_requires?

pythongithubsetuptools

提问by Eric P

I am creating a setup.pyfile 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 setuptoolsinstead of distutilsbecause the latter does not support the install_requiresand dependency_linksarguments per thisanswer.

我使用setuptools而不是distutils因为后者不支持这个答案的install_requiresdependency_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 setuptoolsauthenticate.

上面的设置文件无法访问私有存储库并出现 404 错误 - 这是意料之中的,因为 GitHub 向未授权的私有存储库请求返回 404。但是,我无法弄清楚如何进行setuptools身份验证。

Here are some things I've tried:

以下是我尝试过的一些事情:

  1. Use git+ssh://instead of https://in dependency_linksas I would if installing the repo with pip. This fails because setuptools doesn't recognize this protocol ("unknown url type: git+ssh"), though the distribute documentationsays it should. Ditto git+httpsand git+http.

  2. https://<username>:<password>@github.com/...- still get a 404. (This method doesn't work with curlor wgetfrom the command line either - though curl -u <username> <repo_url> -O <output_file_name>does work.)

  3. 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.

  1. 使用git+ssh://的,而不是https://dependency_links为我想如果安装与回购pip。这失败是因为 setuptools 无法识别此协议(“未知 url 类型:git+ssh”),尽管分发文档说它应该。同上git+httpsgit+http

  2. https://<username>:<password>@github.com/...- 仍然得到 404。(此方法也不适用于命令行curlwget从命令行运行 - 尽管curl -u <username> <repo_url> -O <output_file_name>确实有效。)

  3. 将 setuptools (0.9.7) 和 virtualenv (1.10) 升级到最新版本。还尝试安装分发,尽管此概述说它已合并回 setuptools。无论哪种方式,都没有骰子。

Currently I just have setup.pyprint 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 PEP508standard 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.2on Python 3.7.4

请注意; 这与pip 20.0.2Python 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 -esettings 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
  • versioncan be a branch, a tag, or a commit hash
  • You need to supply --process-dependency-linksif 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:

这适用于我们的场景:

  1. package is on github in a private repo
  2. we want to install it into site-packages (not into ./src with -e)
  3. being able to use pip install -r requirements.txt
  4. being able to use pip install -e reposdir (or from github), where the dependencies are only specified in requirements.txt
  1. 软件包位于 github 上的私有仓库中
  2. 我们想将它安装到站点包中(而不是使用 -e 安装到 ./src 中)
  3. 能够使用 pip install -r requirements.txt
  4. 能够使用 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']