Python setuptools: 如何在 install_need 下列出私有存储库?

我正在为一个依赖于私有 GitHub 存储库的项目创建一个 setup.py文件。文件的相关部分如下:

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/',
],
...,
)

我使用的是 setuptools而不是 distutils,因为后者不支持每个 这个答案的 install_requiresdependency_links参数。

上面的安装文件无法访问私有回购协议,出现了404错误——这是可以预料的,因为 GitHub 对未经授权的私有存储库请求返回了404。但是,我不知道如何使 setuptools进行身份验证。

以下是我尝试过的一些方法:

  1. dependency_links中使用 git+ssh://而不是 https://,就像我使用 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)和 viralenv (1.10)升级到最新版本。尽管 本概述说它已经被合并到 setuptools 中,但是它还是尝试安装 distribution。不管怎样,都没戏。

目前,我只是有 setup.py打印出一个警告,私人回购必须单独下载。这显然不太理想。我觉得我漏掉了一些很明显的东西但我想不起来会是什么。:)

没有答案的重复问题。

43970 次浏览

Edit: This appears to only work with public github repositories, see comments.

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.

Here's what worked for me:

  install_requires=[
'private_package_name==1.1',
],
dependency_links=[
'git+ssh://git@github.com/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.

Using archive URL from github works for me, for public repositories. E.g.

dependency_links = [
'https://github.com/username/reponame/archive/master.zip#egg=eggname-version',
]

I found a (hacky) workaround:

#!/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.

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.

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:

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 some version 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

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

https://github.com/pypa/pip/issues/3610#issuecomment-356687173

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

Please note; this is with pip 20.0.2 on Python 3.7.4

setup(
name='<package>',
...
install_requires=[
'<normal_dependency>',
# Private repository
'<dependency_name> @ git+ssh://git@github.com/<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).

References [1] https://github.com/pypa/pip/issues/4187 [2] https://github.com/pypa/pip/issues/5566

Via Tom Hemmes' answer I found this is the only thing that worked for me:

    install_requires=[
'<package> @ https://github.com/<username>/<package>/archive/<branch_name>.zip']

With pip 20.1.1, this works for me

install_requires=[ "packson3@https://tracinsy.ewi.tudelft.nl/pubtrac/Utilities/export/138/packson3/dist/packson3-1.0.0.tar.gz"],

in setup.py