【问题标题】:How can I use git repos as dependencies for my PyPi package?如何使用 git repos 作为我的 PyPi 包的依赖项?
【发布时间】:2019-07-20 02:27:23
【问题描述】:

我有一个要推送到 PyPi 的包,其中一些依赖项不是包,而是可安装的 git 存储库。我的requirements.txt 是这样的

sphinx_bootstrap_theme>=0.6.5
matplotlib>=2.2.0
numpy>=1.15.0
sphinx>=1.7.5
sphinx-argparse>=0.2.2
tensorboardX
tqdm>=4.24.0
Cython>=0.28.5

# git repos
git+git://github.com/themightyoarfish/svcca-gpu.git

据此,我的setup.py有这样的内容:

#!/usr/bin/env python

from distutils.core import setup
import setuptools
import os

with open('requirements.txt', mode='r') as f:
    requirements = f.read()
    required_pkgs, required_repos = requirements.split('# git repos')
    required_pkgs = required_pkgs.split()
    required_repos = required_repos.split()

with open('README.md') as f:
    readme = f.read()

setup(name=...
      ...
      packages=setuptools.find_packages('.', include=[...]),
      install_requires=required_pkgs,
      dependency_links=required_repos,
      zip_safe=False,   # don't install egg, but source
)

但是运行pip install <package> 并没有真正安装 git 依赖。我假设pip 实际上并没有使用安装脚本。当我手动运行python setup.py install 时它可以工作。

编辑

我还尝试删除 dependency_links 并仅将 install_requires 与存储库一起使用,但是当从 GitHub 安装我的存储库(包括上述文件的项目)时,我遇到了

    Complete output from command python setup.py egg_info:
error in ikkuna setup command: 'install_requires' must be a string or 
list of strings containing valid project/version requirement specifiers; Invalid requirement, parse error at "'+git://g'"

在其他答案中有人建议可以放类似的东西

git+https://github.com/themightyoarfish/svcca-gpu.git#egg=svcca

进入requirements.txt,但失败了

   error in <pkg> setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Invalid requirement, parse error at "'+https:/'

问题:(如何)我可以将 git 存储库列为 pip 包的依赖项吗?

【问题讨论】:

  • 链接的问题和最佳答案似乎没有解决问题,因为它处理需求文件,而不是pip。这个想法必须以setup() 理解的方式陈述需求。
  • 我应该更清楚地说明requirements.txt 在这种情况下只是setuptools 依赖项的代理。

标签: python git pip pypi


【解决方案1】:

在为 Pip 指定 git 依赖项的 50 种左右不同的方法中,唯一能做到我想要的就是这个(PEP 508 中的概述):

svcca @ git+ssh://git@github.com/themightyoarfish/svcca-gpu

可以在install_requires中使用,解决了dependency_links被pip忽略的问题。

一个有趣的副作用是无法将包上传到具有这种依赖关系的 PyPi:

HTTPError: 400 Client Error: Invalid value for requires_dist. Error: Can't have direct dependency: 'svcca @ git+ssh://git@github.com/themightyoarfish/svcca-gpu' for url: https://upload.pypi.org/legacy/

【讨论】:

  • 我被困在“错误:不能有直接依赖:”问题上,不知道如何解决它:-(
  • “不要使用 PyPi”是唯一的解决方案,直到他们改变这一点。
  • 不得不向 PyPi 发布一个包。所以我最终发布了两个包:首先是直接依赖,然后在将直接依赖转换为常规依赖后释放我的实际包!
【解决方案2】:

根据下一篇与How to state in requirements.txt a direct github source相关的帖子。 您可以使用以下语法从 git 远程存储库添加

-e git://github.com/themightyoarfish/svcca-gpu.git

参考: Install a project 处于可编辑模式(即 setuptools “开发模式”),来自本地项目路径或带有-e 的 VCS url

【讨论】:

  • 我不想使用可编辑模式,但如果没有-e,它似乎无法工作。
  • 查看已编辑的问题;这不起作用,因为install_requires' must be a string or list of strings containing valid project/version requirement specifiers;,显然不是。
  • 你好桨鱼!我真的建议您遵循 kennethreitz 创建的下一个结构。在这个存储库中,您将看到如何创建一个很棒的 Setup.py
  • 也许,你应该分割一个休息空间,像这样:required_pkgs = required_pkgs.splitlines()Reference
  • 我真的看不出你链接的setup.py文件是如何处理非pypi包的。
猜你喜欢
  • 2020-03-08
  • 2018-07-27
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 2020-09-28
  • 2012-08-25
相关资源
最近更新 更多